Skip to content

Commit aab5794

Browse files
committed
Chapter 6.2 - Grammarly + Upper Letter Titles
1 parent 19a57e9 commit aab5794

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

chapter-06-nested-loops-exam-problems.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Chapter 6.2. Nested Loops – Exam problems
1+
# Chapter 6.2. Nested Loops – Exam Problems
22

33
In the previous chapter we got familiar with **nested loops** and to use them **drawing** different kinds of **figures on the console**. We learned how to print different size figures, inventing an appropriate logic to construct them using **single and nested** **`for`** loops in combination with various calculations and program logic:
44

@@ -12,7 +12,7 @@ for (int row = 1; row <= 5; row++) {
1212
}
1313
```
1414

15-
We also declared our own method **`repeatStr(…)`** which helps us to print **a given symbol** (or a series of characters) **certain** number of times:
15+
We also declared our method **`repeatStr(…)`** which helps us to print **a given symbol** (or a series of characters) a **certain** number of times:
1616

1717
![](assets/chapter-6-2-images/00.RepeatStr-custom-method-01.png)
1818

@@ -22,7 +22,7 @@ We also declared our own method **`repeatStr(…)`** which helps us to print **a
2222
Let's solve several nested loops related exam problems to practice what we have learned so far and to develop our further algorithmic thinking.
2323

2424

25-
### Problem: Drawing a Fort
25+
### Problem: Draw a Fort
2626

2727
Write a program that reads from the console **an integer n** and draws **a fortress** with width **2 * n columns** and height **n rows** like the examples below. The left and right columns on the inside are **n / 2** wide.
2828

@@ -46,7 +46,7 @@ Print on the console **n** text rows, depicting **the fortress**, exactly like t
4646

4747
### Hints and Guidelines
4848

49-
From the problem statement we see that the **input data** will be one line only containing **an integer** within the range [**3 … 1000**]. Therefore, we will use a **variable** of type **`int`**.
49+
From the problem statement, we see that the **input data** will be one line only containing **an integer** within the range [**3 … 1000**]. Therefore, we will use a **variable** of type **`int`**.
5050

5151
![](assets/chapter-6-2-images/01.Draw-fort-01.png)
5252

@@ -55,7 +55,7 @@ Once we have declared and initialized the input data, we need to divide **the fo
5555
* body
5656
* base
5757

58-
We can see from the examples that **the roof** is made of **two towers** and **a middle part**. Each tower has a beginning **`/`**, a middle part **`^`** and an end **`\`**.
58+
We can see from the examples that **the roof** is made of **two towers** and **a middle part**. Each tower has a beginning **`/`**, a middle part **`^`**, and an end **`\`**.
5959

6060
<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
6161
<td><strong><code>\</code></strong> is a special symbol in Java. Using it in the <strong><code>System.out.println(…)</code></strong> method, the console will not print it. That's why using <strong><code>\\</code></strong> we instruct the console that we want to print exactly this symbol, without being interpreted as a special one (<b>it is shielded</b>- this is called “<b>character escaping</b>”).</td>
@@ -65,25 +65,25 @@ The size of the middle part is **`n / 2`**, therefore we can write this value in
6565

6666
![](assets/chapter-6-2-images/01.Draw-fort-02.png)
6767

68-
Now we declare a second **variable**, where we will store the **value** of **between the two towers** part. The middle roof part has size of **`2 * n - 2 * colSize - 4`**.
68+
Now we declare a second **variable**, where we will store the **value** of **space between the the two towers**. The middle roof part has size of **`2 * n - 2 * colSize - 4`**.
6969

7070
![](assets/chapter-6-2-images/01.Draw-fort-03.png)
7171

72-
In order to print the **roof** on the console, we will use our **`repeatStr(…)`** method, which accepts two parameters **`(string, int)`** and concatenate a certain symbol (or series of characters) **n** times.
72+
To print the **roof** on the console, we will use our **`repeatStr(…)`** method, which accepts two parameters **`(string, int)`** and concatenate a certain symbol (or series of characters) **n** times.
7373

7474
![](assets/chapter-6-2-images/01.Draw-fort-04.png)
7575

76-
**The fort body** contains a beginning **`|`**, a middle part **`(white space)`** and an end **`|`**. **The middle part** is a blank space with size of **`2 * n – 2`**. The number of the **rows** used for walls could be found by the given parameters - **`n - 3`**.
76+
**The fort body** contains a beginning **`|`**, a middle part **`(white space)`**, and an end **`|`**. **The middle part** is a blank space with a size of **`2 * n – 2`**. The number of **rows** used for walls could be found by the given parameters - **`n - 3`**.
7777

7878
![](assets/chapter-6-2-images/01.Draw-fort-05.png)
7979

80-
In order to draw the last row, which is a part of the base, we need to print the beginning **`|`**, the middle part **`(white space)_(white space)`** and an end **`|`**. To do this, we can use the declared **`colSize`** and **`midSize`** variables, because we can see from the examples that they are equal to the **`_`** symbol in the roof.
80+
To draw the last row, which is a part of the base, we need to print the beginning **`|`**, the middle part **`(white space)_(white space)`**, and an end **`|`**. To do this, we can use the declared **`colSize`** and **`midSize`** variables, because we can see from the examples that they are equal to the **`_`** symbol in the roof.
8181

8282
![](assets/chapter-6-2-images/01.Draw-fort-06.png)
8383

84-
We add to the values of the **blank spaces** **`+ 1`**, because we have **one** blank space more in the examples.
84+
We add to the values of the **blank spaces** **`+ 1`** because we have **one** blank space more in the examples.
8585

86-
The **fort’s base** structure is same as the **roof** one. It is made of **two towers** and a **middle part**. Each **tower** has a beginning **`\`**, a middle part **`_`** and an end **`/`**.
86+
The **fort’s base** structure is the same as the **roof** one. It is made of **two towers** and a **middle part**. Each **tower** has a beginning **`\`**, a middle part **`_`**, and an end **`/`**.
8787

8888
![](assets/chapter-6-2-images/01.Draw-fort-07.png)
8989

@@ -116,23 +116,23 @@ Print **2 * (n - 2) + 1** text lines, representing the **butterfly** on the cons
116116

117117
### Hints and Guidelines
118118

119-
From the problem statement we see that the **input data** will be read from only one line, which will contain an **integer** in the range [**3 … 1000**]. For this reason, we will use **a variable** of type **`int`**.
119+
From the problem statement, we see that the **input data** will be read from only one line, which will contain an **integer** in the range [**3 … 1000**]. For this reason, we will use **a variable** of type **`int`**.
120120

121121
![](assets/chapter-6-2-images/02.Butterfly-01.png)
122122

123-
We can divide the figure into 3 parts - **upper wing**, **body** and **lower wing** . In order to draw the upper wing, we need to divide it into parts - a beginning **`*`**, a middle part **`\ /`** and an end **`*`**. After looking at the examples we can say that the beginning has size **`n - 2`**.
123+
We can divide the figure into 3 parts - **upper wing**, **body**, and **lower wing**. To draw the upper wing, we need to divide it into parts - a beginning **`*`**, a middle part **`\ /`** and an end **`*`**. After looking at the examples we can say that the beginning has size **`n - 2`**.
124124

125125
![](assets/chapter-6-2-images/02.Butterfly-02.png)
126126

127127
We also see that the upper wing of the butterfly is of size **`n - 2`**, so we can make a loop that iterates **`halfRowSize`** times.
128128

129129
![](assets/chapter-6-2-images/02.Butterfly-03.png)
130130

131-
From the examples we can notice that on an **even** row we have a beginning **`-`**, a middle part **`\ /`** and an end **`*`**, and on an **odd** line - a beginning **`*`**, a middle part **`\ /`** and an end **`-`**. Therefore, we need to do an **`if-else`** condition to check whether the line is even or odd and print one of the two types of lines accordingly.
131+
From the examples, we can notice that on an **even** row we have a beginning **`-`**, a middle part **`\ /`** and an end **`*`**, and on an **odd** line - a beginning **`*`**, a middle part **`\ /`** and an end **`-`**. Therefore, we need to do an **`if-else`** condition to check whether the line is even or odd and print one of the two types of lines accordingly.
132132

133133
![](assets/chapter-6-2-images/02.Butterfly-04.png)
134134

135-
To create the **body of the butterfly** , we can use again the **`halfRowSize`** **variable** and print exactly **one** line on the console. The body structure has a beginning **`(white space)`**, a middle **`@`** and an end **`(white space)`**.
135+
To create the **body of the butterfly**, we can use again the **`halfRowSize`** **variable** and print exactly **one** line on the console. The body structure has a beginning **`(white space)`**, a middle **`@`**, and an end **`(white space)`**.
136136

137137
![](assets/chapter-6-2-images/02.Butterfly-05.png)
138138

@@ -145,7 +145,7 @@ It remains to print the lower wing on the console, which is identical to the upp
145145
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/658#1](https://judge.softuni.org/Contests/Practice/Index/658#1).
146146

147147

148-
### Problem: "Stop" Sign
148+
### Problem: Stop
149149

150150
Write a program that reads an **integer n** from the console and draws a **STOP warning sign** with size as in the example below.
151151

@@ -169,11 +169,11 @@ Print text lines on the console depicting the **STOP warning sign**, just as in
169169

170170
### Hints and Guidelines
171171

172-
From the problem statement we see that **the input data** will be read from only one line which contains an **integer** in the range of [**3 … 1000**]. Therefore, we will use a **variable** of type **`int`**.
172+
From the problem statement, we see that **the input data** will be read from only one line which contains an **integer** in the range of [**3 … 1000**]. Therefore, we will use a **variable** of type **`int`**.
173173

174174
![](assets/chapter-6-2-images/03.Stop-01.png)
175175

176-
We can **divide** the figure into **3 parts** - upper, middle and lower. **The upper part** consists of two sub-parts - an initial line and lines in which the character expands. **The start row** is composed of a beginning **`.`**, a middle part **`_`** and an end **`.`**. After looking at the examples we can say that the beginning is of size **`n + 1`** and it is better to to separate this **value** into a separate **variable**.
176+
We can **divide** the figure into **3 parts** - upper, middle, and lower. **The upper part** consists of two sub-parts - an initial line and lines in which the character expands. **The start row** is composed of a beginning **`.`**, a middle part **`_`** and an end **`.`**. After looking at the examples we can say that the beginning is of size **`n + 1`** and it is better to separate this **value** into a separate **variable**.
177177

178178
![](assets/chapter-6-2-images/03.Stop-02.png)
179179

@@ -185,19 +185,19 @@ Once we have declared and initialized the two variables, we can print the start
185185

186186
![](assets/chapter-6-2-images/03.Stop-04.png)
187187

188-
In order to draw the lines where the sign is getting **"wider"**, we have to create **a loop**, that iterates **`n`** number of times. The structure of a line consists of a beginning **`.`**, **`//`** + middle part **`_`** + **`\\`** and an end **`.`**. In order to be able to reuse the created **variables** , we need to decrease the **`dots`** by 1 and the **`underscores`** by 2, because we have already **printed** the first row, and the dots and underscores in the next line of the figure **decrease**.
188+
To draw the lines where the sign is getting **"wider"**, we have to create **a loop**, that iterates **`n`** number of times. The structure of a line consists of a beginning **`.`**, **`//`** + middle part **`_`** + **`\\`**, and an end **`.`**. To be able to reuse the created **variables**, we need to decrease the **`dots`** by 1 and the **`underscores`** by 2, because we have already **printed** the first row and the dots and underscores in the next line of the figure **decrease**.
189189

190190
![](assets/chapter-6-2-images/03.Stop-05.png)
191191

192-
At each subsequent iteration the **beginning** and the **end** decreasing with 1, and the **middle part** increases by 2.
192+
At each subsequent iteration the **beginning** and the **end** decrease with 1, and the **middle part** increases by 2.
193193

194194
![](assets/chapter-6-2-images/03.Stop-06.png)
195195

196196
**The middle part** of the figure has a beginning **`//`** + **`_`**, a middle part **`STOP!`** and an end **`_`** + **`\\`**. The count of the underscores **`_`** is **`(underscores - 5) / 2`**.
197197

198198
![](assets/chapter-6-2-images/03.Stop-07.png)
199199

200-
**The lower part** of the figure where the sign **decreases**, can be done by creating a **loop** again, which iterates **`n`** number of times. The structure of a single line is a beginning **`.`** + **`\\`**, a middle part **`_`** and an end **`//`** + **`.`**. The number of the **dots** in the first iteration of the loop should be 0 and **increases** by one on each subsequent iteration. Therefore, we can say that the size of the **dots in the lower part of the figure** is equal to **`i`**.
200+
**The lower part** of the figure where the sign **decreases** can be done by creating a **loop** again, which iterates **`n`** number of times. The structure of a single line is a beginning **`.`** + **`\\`**, a middle part **`_`** and an end **`//`** + **`.`**. The number of the **dots** in the first iteration of the loop should be 0 and **increase** by one on each subsequent iteration. Therefore, we can say that the size of the **dots in the lower part of the figure** is equal to **`i`**.
201201

202202
![](assets/chapter-6-2-images/03.Stop-08.png)
203203

@@ -238,7 +238,7 @@ From the problem condition, we see that **the input data** will be read from onl
238238

239239
![](assets/chapter-6-2-images/04.Arrow-01.png)
240240

241-
We can divide the figure into **3 parts** - upper, middle and lower. **The upper part** consists of two subparts – the initial row and the arrow body. From the examples we see that the number of the **outer dots** in the initial row and in the arrow body are **`(n - 1) / 2`**. We can store this value in an **`outerDots`** **variable**.
241+
We can divide the figure into **3 parts** - upper, middle, and lower. **The upper part** consists of two subparts – the initial row and the arrow body. From the examples, we see that the number of the **outer dots** in the initial row and in the arrow body are **`(n - 1) / 2`**. We can store this value in an **`outerDots`** **variable**.
242242

243243
![](assets/chapter-6-2-images/04.Arrow-02.png)
244244

@@ -250,23 +250,23 @@ We can see from the examples the structure of the initial line. We must use the
250250

251251
![](assets/chapter-6-2-images/04.Arrow-04.png)
252252

253-
In order to draw on the **arrow's body** on the console , we have to create a **loop**, which iterates **`n - 2`** times.
253+
To draw on the **arrow's body** on the console, we have to create a **loop**, which iterates **`n - 2`** times.
254254

255255
![](assets/chapter-6-2-images/04.Arrow-05.png)
256256

257257
**The middle part of the figure** consists of a beginning **`#`**, a middle part **`.`** and an end **`#`**. We see from the examples that the number of the **`#`** is equal to **`outerDots`** increased by 1 and that is why we can use the same **variable** again.
258258

259259
![](assets/chapter-6-2-images/04.Arrow-06.png)
260260

261-
To draw the **the arrow’s lower part**, we need to assign new values to the **variables** **`outerDots`** and **`innerDots`**.
261+
To draw the **arrow’s lower part**, we need to assign new values to the **variables** **`outerDots`** and **`innerDots`**.
262262

263263
![](assets/chapter-6-2-images/04.Arrow-07.png)
264264

265-
**The loop**, we are going to make must iterates **`n - 2`**times and we need to print the last row of the figure separately. On each iteration **`outerDots`** increases by 1, and **`innerDots`** decreases by 2.
265+
**The loop**, we are going to make must iterates **`n - 2`**times and we need to print the last row of the figure separately. On each iteration **`outerDots`** increase by 1, and **`innerDots`** decrease by 2.
266266

267267
![](assets/chapter-6-2-images/04.Arrow-08.png)
268268

269-
**The last row** of our figure is made of a beginning **`.`**, a middle part **`#`** and an end **`.`**. The number of the **`.`** is equal to **`outerDots`**.
269+
**The last row** of our figure is made of a beginning **`.`**, a middle part **`#`**, and an end **`.`**. The number of the **`.`** is equal to **`outerDots`**.
270270

271271
![](assets/chapter-6-2-images/04.Arrow-09.png)
272272

@@ -300,15 +300,15 @@ Print on the console an **axe**, exactly like the examples.
300300

301301
### Hints and Guidelines
302302

303-
In order to solve the problem, we first need to calculate the **dashes on the left, the middle dashes, the dashes on the right** and the whole length of the figure.
303+
To solve the problem, we first need to calculate the **dashes on the left, the middle dashes, the dashes on the right**, and the whole length of the figure.
304304

305305
![](assets/chapter-6-2-images/05.Axe-01.png)
306306

307-
Once we have declared and initialized the **variables**, we can begin drawing the figure by starting with the **upper part**. From the examples, we can figure out the structure of the **first row** and to create a loop that iterates **`n`** number of times. At each iteration of the loop the **middle dashes** increase by 1, and the **dashes on the right** decrease by 1.
307+
Once we have declared and initialized the **variables**, we can begin drawing the figure by starting with the **upper part**. From the examples, we can figure out the structure of the **first row**, and create a loop that iterates **`n`** number of times. At each iteration, of the loop the **middle dashes** increase by 1, and the **dashes on the right** decrease by 1.
308308

309309
![](assets/chapter-6-2-images/05.Axe-02.png)
310310

311-
In order to be able to reuse the created **variables** when drawing the axe’s handle, we need to decrease the **middle dashes** by 1, and increase the **dashes on the right** by 1.
311+
To be able to reuse the created **variables** when drawing the axe’s handle, we need to decrease the **middle dashes** by 1 and increase the **dashes on the right** by 1.
312312

313313
![](assets/chapter-6-2-images/05.Axe-03.png)
314314

@@ -320,7 +320,7 @@ We can draw the **handle of the axe** by iterating the loop **`n - 2`** times. F
320320

321321
![](assets/chapter-6-2-images/05.Axe-05.png)
322322

323-
For the **last row** of the figure we can use the three declared and initialized variables **`leftDashes`**, **`middleDashes`**, **`rightDashes`** again.
323+
For the **last row** of the figure, we can use the three declared and initialized variables **`leftDashes`**, **`middleDashes`**, **`rightDashes`** again.
324324

325325
![](assets/chapter-6-2-images/05.Axe-06.png)
326326

0 commit comments

Comments
 (0)