You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter-06-nested-loops-exam-problems.md
+30-30Lines changed: 30 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Chapter 6.2. Nested Loops – Exam problems
1
+
# Chapter 6.2. Nested Loops – Exam Problems
2
2
3
3
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:
@@ -22,7 +22,7 @@ We also declared our own method **`repeatStr(…)`** which helps us to print **a
22
22
Let's solve several nested loops related exam problems to practice what we have learned so far and to develop our further algorithmic thinking.
23
23
24
24
25
-
### Problem: Drawing a Fort
25
+
### Problem: Draw a Fort
26
26
27
27
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.
28
28
@@ -46,7 +46,7 @@ Print on the console **n** text rows, depicting **the fortress**, exactly like t
46
46
47
47
### Hints and Guidelines
48
48
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`**.
@@ -55,7 +55,7 @@ Once we have declared and initialized the input data, we need to divide **the fo
55
55
* body
56
56
* base
57
57
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 **`\`**.
<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
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`**.
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.
**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`**.
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.
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.
85
85
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 **`/`**.
@@ -116,23 +116,23 @@ Print **2 * (n - 2) + 1** text lines, representing the **butterfly** on the cons
116
116
117
117
### Hints and Guidelines
118
118
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`**.
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`**.
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.
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)`**.
@@ -145,7 +145,7 @@ It remains to print the lower wing on the console, which is identical to the upp
145
145
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/658#1](https://judge.softuni.org/Contests/Practice/Index/658#1).
146
146
147
147
148
-
### Problem: "Stop" Sign
148
+
### Problem: Stop
149
149
150
150
Write a program that reads an **integer n** from the console and draws a **STOP warning sign** with size as in the example below.
151
151
@@ -169,11 +169,11 @@ Print text lines on the console depicting the **STOP warning sign**, just as in
169
169
170
170
### Hints and Guidelines
171
171
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`**.
173
173
174
174

175
175
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**.
177
177
178
178

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

187
187
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**.
189
189
190
190

191
191
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.
193
193
194
194

195
195
196
196
**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`**.
197
197
198
198

199
199
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`**.
201
201
202
202

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

240
240
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**.
242
242
243
243

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

252
252
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.
254
254
255
255

256
256
257
257
**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.
258
258
259
259

260
260
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`**.
262
262
263
263

264
264
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.
266
266
267
267

268
268
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`**.
270
270
271
271

272
272
@@ -300,15 +300,15 @@ Print on the console an **axe**, exactly like the examples.
300
300
301
301
### Hints and Guidelines
302
302
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.
304
304
305
305

306
306
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.
308
308
309
309

310
310
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.
312
312
313
313

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

322
322
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.
0 commit comments