diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md index 086d4dd73..5c54122b5 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md @@ -1,4 +1,4 @@ -### Example: Diamond +# Problem: Diamond Write a program that takes an integer **n** (1 ≤ **n** ≤ 100) and prints a diamond with size **n**, as in the following examples: @@ -13,3 +13,76 @@ Write a program that takes an integer **n** (1 ≤ **n** ≤ 100) and prints a d |Input|Output|Input|Output|Input|Output| |---|---|---|---|---|---| |7|---\*---
--\*-\*--
-\*---\*-
\*-----\*
-\*---\*-
--\*-\*--
---\*---
|8|---\*\*---
--\*--\*--
-\*----\*-
\*------\*
-\*----\*-
--\*--\*--
---\*\*---
|9|----\*----
---\*-\*---
--\*---\*--
-\*-----\*-
\*-------\*
-\*-----\*-
--\*---\*--
---\*-\*---
----\*----| + +## Hints and Guidelines + +What we know from the problem explanation is that the diamond is with size **`n` x `n`**. + +From the example input and output we can conclude that all rows contain exactly **`n`** symbols, and all the rows, with the exception of the top and bottom ones, have **2 stars**. We can mentally divide the diamond into 2 parts: +* **Upper** part. It starts from the upper tip down to the middle. +* **Lower** part. It starts from the row below the middle one and goes down to the lower tip (inclusive). + +### Upper Part + +* If **n** is an **odd** number, it starts with **1 star**. +* If **n** is an **even** number, it starts with **2 stars**. +* With each row down, the stars get further away from each other. +* The space between, before and after **the stars** is filled up with **dashes**. + +### Lower Part + +* With each row down, the stars get closer to each other. This means that the space (**the dashes**) between them is getting smaller and the space (**the dashes**) on the left and on the right is getting larger. +* The bottom-most part has 1 or 2 **stars**, depending on whether **n** is an even or odd number. + +#### Upper and Lower Parts of the Diamond + +* On each row, except the middle one, the stars are surrounded by inner and outer **dashes**. +* On each row there is space between the two **stars**, except on the first and the last row (sometimes **the star is 1**). + +### Reading the Input Data + +We read **n** from the console and we save it in a variable of **`int`** type. + +![](/assets/chapter-6-images/10.Diamond-01.png) + +### Printing the Top Part of the Diamond + +We start drawing the upper part of the diamond. The first thing we need to do is to calculate the number of the outer **dashes `leftRight`** (the dashes on the outer side of **the stars**). It is equal to **`(n - 1) / 2`**, rounded down. + +![](/assets/chapter-6-images/10.Diamond-02.png) + +After we have calculated **`leftRight`**, we start drawing **the upper part** of the diamond. We can start by running a **loop** from **`0`** to **`n / 2 + 1`** (rounded down). + +At each iteration of the loop the following steps must be taken: +* We draw on the console the left **dashes** (with length **`leftRight`**) and right after them the first **star**. + +![](/assets/chapter-6-images/10.Diamond-03.png) + +* We will calculate the distance between the two **stars**. We can do this by subtracting from **n** the number of the outer **dashes**, and the number 2 (the number of **the stars**, i.e. the diamonds outline). We need to store the result of the subtraction in a variable **`mid`**. + +![](/assets/chapter-6-images/10.Diamond-04.png) + +* If **`mid`** is lower than 0, we know that on the row there should be only 1 star. If it is higher or equal to 0 then we have to print **dashes** with length **`mid`** and one **star** after them. +* We draw on the console the right outer **dashes** with length **`leftRight`**. + +![](/assets/chapter-6-images/10.Diamond-05.png) + +* In the end of the loop we decrease **`leftRight`** by 1 (**the stars** are moving away from each other). + +We are ready with the upper part. + +### Printing the Bottom Part of the Diamond + +Printing the lower part is very similar to that of the upper part. The difference is that instead of decreasing **`leftRight`** with 1 in the end of the loop, we will increase **`leftRight`** with 1 at the beginning of the loop. Also, **the loop will be from 0 to `(n - 1) / 2`**. + +![](/assets/chapter-6-images/10.Diamond-01.png) + + + +
Repeating a code is considered bad practice, because the code becomes very hard to maintain. Let's imagine that we have a piece of code (e.g. the logic for drawing a row from the diamond) at a few more places and we decide to change it. For this we will have to go through all the places and change it everywhere. Now let's imagine that you need to reuse a piece of code not 1, 2 or 3 times but tens of times. A way to overcome this problem is to use methods. You can look for additional information for methods in the Internet or to look at Chapter “10” (Methods).
+ +If we have written all correctly, then the problem is solved. + +## Testing in the Judge System + +Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#9](https://judge.softuni.bg/Contests/Practice/Index/512#9). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md index 51018967f..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md @@ -1,21 +0,0 @@ -#### Hints and Guidelines - -What we know from the problem explanation is that the diamond is with size **`n` x `n`**. - -From the example input and output we can conclude that all rows contain exactly **`n`** symbols, and all the rows, with the exception of the top and bottom ones, have **2 stars**. We can mentally divide the diamond into 2 parts: -* **Upper** part. It starts from the upper tip down to the middle. -* **Lower** part. It starts from the row below the middle one and goes down to the lower tip (inclusive). - -##### Upper Part -* If **n** is an **odd** number, it starts with **1 star**. -* If **n** is an **even** number, it starts with **2 stars**. -* With each row down, the stars get further away from each other. -* The space between, before and after **the stars** is filled up with **dashes**. - -##### Lower Part -* With each row down, the stars get closer to each other. This means that the space (**the dashes**) between them is getting smaller and the space (**the dashes**) on the left and on the right is getting larger. -* The bottom-most part has 1 or 2 **stars**, depending on whether **n** is an even or odd number. - -##### Upper and Lower Parts of the Diamond -* On each row, except the middle one, the stars are surrounded by inner and outer **dashes**. -* On each row there is space between the two **stars**, except on the first and the last row (sometimes **the star is 1**). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md index 806cca5d2..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md @@ -1,15 +0,0 @@ -#### Printing the Bottom Part of the Diamond - -Printing the lower part is very similar to that of the upper part. The difference is that instead of decreasing **`leftRight`** with 1 in the end of the loop, we will increase **`leftRight`** with 1 at the beginning of the loop. Also, **the loop will be from 0 to `(n - 1) / 2`**. - -![](/assets/chapter-6-images/10.Diamond-01.png) - - - -
Repeating a code is considered bad practice, because the code becomes very hard to maintain. Let's imagine that we have a piece of code (e.g. the logic for drawing a row from the diamond) at a few more places and we decide to change it. For this we will have to go through all the places and change it everywhere. Now let's imagine that you need to reuse a piece of code not 1, 2 or 3 times but tens of times. A way to overcome this problem is to use methods. You can look for additional information for methods in the Internet or to look at Chapter “10” (Methods).
- -If we have written all correctly, then the problem is solved. - -#### Testing in the Judge System - -Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#9](https://judge.softuni.bg/Contests/Practice/Index/512#9). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md index 6a9eef37a..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md @@ -1,29 +0,0 @@ -#### Reading the Input Data and Printing the Top Part of the Diamond - -We read **n** from the console and we save it in a variable of **`int`** type. - -![](/assets/chapter-6-images/10.Diamond-01.png) - -We start drawing the upper part of the diamond. The first thing we need to do is to calculate the number of the outer **dashes `leftRight`** (the dashes on the outer side of **the stars**). It is equal to **`(n - 1) / 2`**, rounded down. - -![](/assets/chapter-6-images/10.Diamond-02.png) - -After we have calculated **`leftRight`**, we start drawing **the upper part** of the diamond. We can start by running a **loop** from **`0`** to **`n / 2 + 1`** (rounded down). - -At each iteration of the loop the following steps must be taken: -* We draw on the console the left **dashes** (with length **`leftRight`**) and right after them the first **star**. - -![](/assets/chapter-6-images/10.Diamond-03.png) - -* We will calculate the distance between the two **stars**. We can do this by subtracting from **n** the number of the outer **dashes**, and the number 2 (the number of **the stars**, i.e. the diamonds outline). We need to store the result of the subtraction in a variable **`mid`**. - -![](/assets/chapter-6-images/10.Diamond-04.png) - -* If **`mid`** is lower than 0, we know that on the row there should be only 1 star. If it is higher or equal to 0 then we have to print **dashes** with length **`mid`** and one **star** after them. -* We draw on the console the right outer **dashes** with length **`leftRight`**. - -![](/assets/chapter-6-images/10.Diamond-05.png) - -* In the end of the loop we decrease **`leftRight`** by 1 (**the stars** are moving away from each other). - -We are ready with the upper part. diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md index 4fd2f77c4..e8491aaa7 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md @@ -1,3 +1,35 @@ -## Drawing More Complex Figures +# Exercises: Drawing Figures -Let's look at how to draw figures with more complex construction logic in the console, for which we need to start thinking more before starting to write. +Let's look at how to **draw figures** using **nested loops** with more complex logic, for which we need to think more before coding. + +## What We Learned in This Chapter? + +Before starting, let's review what we learned in this chapter. + +We became acquainted with the **`new string`** constructor: + +```csharp +string printMe = new string('*', 5); +``` + +We learned to draw figures with nested **`for`** loops: + +```csharp +for (var r = 1; r <= 5; r++) +{ + Console.Write("*"); + for (var c = 1; c < 5; c++) + Console.Write(" *"); + Console.WriteLine(); +} +``` + +## The Problems + +We will work on the following set of practical problems: + +* [Problem: Rhombus of Stars](/Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md) +* [Problem: Christmas Tree](/Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md) +* [Problem: Sunglasses](/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md) +* [Problem: House](/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md) +* [Problem: Diamond](/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md) diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md index 90f28833a..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md @@ -1,23 +0,0 @@ -#### Calculating Roof Length - -In order to draw **the roof**, we write down how many **stars** we start with in a variable called **`stars`**: -* If **`n`** is **an even** number, there will be 2 stars. -* If it is **odd**, there will be 1. - -![](/assets/chapter-6-images/09.House-03.png) - -Calculate the length of **the roof**. It equals half of **`n`**. Write the result in the variable **`roofLength`**. - -![](/assets/chapter-6-images/09.House-04.png) - -It is important to note that when **`n`** is an odd number, the length of the roof is one row more than that of the **base**. In **C#** when you divide two numbers with a remainder, the result will be the number without remainder. - -Example: - -```csharp -int result = 3 / 2; // result 1 -``` - -If we want to round up, we need to use the method **`Math.Ceiling(…)`**: -**`int result = (int)Math.Ceiling(3 / 2f);`** -In this example the division isn't between two integers. "`f`" after a number shows that this number is of **`float`** type (a floating point number). The result of **`3 / 2f`** is **`1.5f`**. **`Math.Ceiling(…)`** rounds the division up. In this case **`1.5f`** will become 2. **`(int)`** is used so that we can transfer the type back to **`int`**. diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md index 72a308d3b..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md @@ -1,21 +0,0 @@ -#### Hints and Guidelines - -We understand from the problem explanation that the house is with size of **`n` x `n`**. What we see from the example input and output is that: - -* The house is divided into two parts: **roof and base**. - -![](/assets/chapter-6-images/09.House-01.png) - -* When **`n`** is an even number, the point of the house is "dull". -* When **`n`** is odd, **the roof** is one row larger than the **base**. - -##### Roof -* It comprises of **stars** and **dashes**. -* In the top part there are one or two stars, depending on if **n** is even or odd (also related to the dashes). -* In the lowest part there are many stars and no dashes. -* With each lower row, **the stars** increase by 2 and **the dashes** decrease by 2. - -##### Base -* The height is **`n`** rows. -* It is made out of **stars** and **pipes**. -* Each row comprises of 2 **pipes** - one in the beginning and one in the end of the row, and also **stars** between the pipes with string length of **`n - 2`**. diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md index ef972ac72..1c3ab947b 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md @@ -1,4 +1,4 @@ -### Example: House +# Problem: House Write a program that takes a number **n** (2 ≤ **n** ≤ 100) and prints **a house** with size **n x n**, just as in the examples: @@ -9,3 +9,94 @@ Write a program that takes a number **n** (2 ≤ **n** ≤ 100) and prints **a h |Input|Output|Input|Output| |---|---|---|---| |5|--\*--
-\*\*\*-
\*\*\*\*\*
|\*\*\*|
|\*\*\*||8|---\*\*---
--\*\*\*\*--
-\*\*\*\*\*\*-
\*\*\*\*\*\*\*\*
|\*\*\*\*\*\*|
|\*\*\*\*\*\*|
|\*\*\*\*\*\*|
|\*\*\*\*\*\*|
| + +## Hints and Guidelines + +We understand from the problem explanation that the house is with size of **`n` x `n`**. What we see from the example input and output is that: + +* The house is divided into two parts: **roof and base**. + +![](/assets/chapter-6-images/09.House-01.png) + +* When **`n`** is an even number, the point of the house is "dull". +* When **`n`** is odd, **the roof** is one row larger than the **base**. + +### Roof + +* It comprises of **stars** and **dashes**. +* In the top part there are one or two stars, depending on if **n** is even or odd (also related to the dashes). +* In the lowest part there are many stars and no dashes. +* With each lower row, **the stars** increase by 2 and **the dashes** decrease by 2. + +### Base + +* The height is **`n`** rows. +* It is made out of **stars** and **pipes**. +* Each row comprises of 2 **pipes** - one in the beginning and one in the end of the row, and also **stars** between the pipes with string length of **`n - 2`**. + +### Reading the Input Data + +We read **`n`** from the console and we save it in a variable of **`int`** type. + +![](/assets/chapter-6-images/09.House-02.png) + + + +
It is very important to check if the input data is correct! In these tasks it is not a problem to directly convert the data from the console into int type, because it is said that we will receive valid integers. If you are making more complex programs it is a good practice to check the data. What will happen if instead of the character "А" the user inputs a number?
+ +### Calculating Roof Length + +In order to draw **the roof**, we write down how many **stars** we start with in a variable called **`stars`**: +* If **`n`** is **an even** number, there will be 2 stars. +* If it is **odd**, there will be 1. + +![](/assets/chapter-6-images/09.House-03.png) + +Calculate the length of **the roof**. It equals half of **`n`**. Write the result in the variable **`roofLength`**. + +![](/assets/chapter-6-images/09.House-04.png) + +It is important to note that when **`n`** is an odd number, the length of the roof is one row more than that of the **base**. In **C#** when you divide two numbers with a remainder, the result will be the number without remainder. + +Example: + +```csharp +int result = 3 / 2; // result 1 +``` + +If we want to round up, we need to use the method **`Math.Ceiling(…)`**: +**`int result = (int)Math.Ceiling(3 / 2f);`** +In this example the division isn't between two integers. "`f`" after a number shows that this number is of **`float`** type (a floating point number). The result of **`3 / 2f`** is **`1.5f`**. **`Math.Ceiling(…)`** rounds the division up. In this case **`1.5f`** will become 2. **`(int)`** is used so that we can transfer the type back to **`int`**. + +### Printing the Roof + +After we have calculated the length of the roof we make a loop from 0 to **`roofLength`**. On each iteration we will: +* Calculate the number of **dashes** we need to draw. The number will be equal to **`(n - stars) / 2`**. We store it in a variable **`padding`**. + +![](/assets/chapter-6-images/09.House-05.png) + +* We print in the console: "**dashes**" (**`padding / 2`** times) + "**stars**" (**`stars`** times) + "**dashes**" (**`padding / 2`** times). + +![](/assets/chapter-6-images/09.House-06.png) + +* Before the iteration is over we add 2 to **`stars`** (the number of **the stars**). + +![](/assets/chapter-6-images/09.House-07.png) + + + +
It is not a good idea to add many character strings as it is shown above, because this leads to performance issues. Learn more at: https://bg.wikipedia.org/wiki/%D0%9D%D0%B8%D0%B7#String_Builder
+ +### Printing the Base + +After we have finished with the **roof**, it is time for **the base**. It is easier to print: +* We start with a loop from 0 to n (not inclusive). +* We print in the console: `|` + `*` (**`n - 2`** times) + `|`. + +![](/assets/chapter-6-images/09.House-08.png) + +If you have written everything as it is here, the problem should be solved. + +## Testing in the Judge System + +Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#8](https://judge.softuni.bg/Contests/Practice/Index/512#8). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md index 53a630650..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md @@ -1,13 +0,0 @@ -#### Printing the Base - -After we have finished with the **roof**, it is time for **the base**. It is easier to print: -* We start with a loop from 0 to n (not inclusive). -* We print in the console: `|` + `*` (**`n - 2`** times) + `|`. - -![](/assets/chapter-6-images/09.House-08.png) - -If you have written everything as it is here, the problem should be solved. - -#### Testing in the Judge System - -Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#8](https://judge.softuni.bg/Contests/Practice/Index/512#8). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-roof.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-roof.md index 155a99574..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-roof.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-roof.md @@ -1,18 +0,0 @@ -#### Printing the Roof - -After we have calculated the length of the roof we make a loop from 0 to **`roofLength`**. On each iteration we will: -* Calculate the number of **dashes** we need to draw. The number will be equal to **`(n - stars) / 2`**. We store it in a variable **`padding`**. - -![](/assets/chapter-6-images/09.House-05.png) - -* We print in the console: "**dashes**" (**`padding / 2`** times) + "**stars**" (**`stars`** times) + "**dashes**" (**`padding / 2`** times). - -![](/assets/chapter-6-images/09.House-06.png) - -* Before the iteration is over we add 2 to **`stars`** (the number of **the stars**). - -![](/assets/chapter-6-images/09.House-07.png) - - - -
It is not a good idea to add many character strings as it is shown above, because this leads to performance issues. For more information visit: https://bg.wikipedia.org/wiki/%D0%9D%D0%B8%D0%B7#String_Builder
diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/read-input-data.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/read-input-data.md index d72667fe9..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/read-input-data.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/read-input-data.md @@ -1,9 +0,0 @@ -#### Reading the Input Data - -We read **`n`** from the console and we save it in a variable of **`int`** type. - -![](/assets/chapter-6-images/09.House-02.png) - - - -
It is very important to check if the input data is correct! In these tasks it is not a problem to directly convert the data from the console into int type, because it is said that we will receive valid integers. If you are making more complex programs it is a good practice to check the data. What will happen if instead of the character "А" the user inputs a number?
diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-middle-rows.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-middle-rows.md index b54efdf71..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-middle-rows.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-middle-rows.md @@ -1,9 +0,0 @@ -#### Printing the Middle Rows - -When drawing **the middle** part we need to **check** if the row is **`(n-1) / 2 - 1`**, because in the examples we can see that in **this row** we need to print **pipes** instead of white spaces. - -![](/assets/chapter-6-images/08.Sunglasses-02.png) - -#### Testing in the Judge System - -Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#7](https://judge.softuni.bg/Contests/Practice/Index/512#7). diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-top-and-bot-row.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-top-and-bot-row.md index e9b353803..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-top-and-bot-row.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-top-and-bot-row.md @@ -1,7 +0,0 @@ -#### Printing the Top and Bottom Rows - -From the examples we can see that the sunglasses can be divided into **three parts** – upper, middle and lower one. A part of the code with which the problem can be solved is given below. - -When drawing the upper and lower rows we need to print **`2 * n`** stars, **`n`** white spaces and **`2 * n`** stars. - -![](/assets/chapter-6-images/08.Sunglasses-01.png) diff --git a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md index ae222863e..d26ff4213 100644 --- a/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md +++ b/Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md @@ -1,4 +1,5 @@ -### Example: Sunglasses +# Problem: Sunglasses + Write a program that takes an integer **n** (3 ≤ n ≤ 100) and prints sunglasses with size of **5\*n x n** as found in the examples: |Input|Output|Input|Output| @@ -8,3 +9,21 @@ Write a program that takes an integer **n** (3 ≤ n ≤ 100) and prints sunglas |Input|Output| |---|---| |5|\*\*\*\*\*\*\*\*\*\*     \*\*\*\*\*\*\*\*\*\*
\*////////\*     \*////////\*
\*////////\*|||||\*////////\*
\*////////\*     \*////////\*
\*\*\*\*\*\*\*\*\*\*     \*\*\*\*\*\*\*\*\*\*
| + +## Printing the Top and Bottom Rows + +From the examples we can see that the sunglasses can be divided into **three parts** – upper, middle and lower one. A part of the code with which the problem can be solved is given below. + +When drawing the upper and lower rows we need to print **`2 * n`** stars, **`n`** white spaces and **`2 * n`** stars. + +![](/assets/chapter-6-images/08.Sunglasses-01.png) + +## Printing the Middle Rows + +When drawing **the middle** part we need to **check** if the row is **`(n-1) / 2 - 1`**, because in the examples we can see that in **this row** we need to print **pipes** instead of white spaces. + +![](/assets/chapter-6-images/08.Sunglasses-02.png) + +## Testing in the Judge System + +Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#7](https://judge.softuni.bg/Contests/Practice/Index/512#7). diff --git a/Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md b/Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md index ce1e6e2e1..7f8361589 100644 --- a/Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md +++ b/Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md @@ -1,4 +1,4 @@ -### Example: Rectangle Made of 10 x 10 Stars +# Example: Rectangle Made of 10 x 10 Stars Print in the console a rectangle made out of **10 x 10** stars. @@ -6,12 +6,12 @@ Print in the console a rectangle made out of **10 x 10** stars. |---|---| |(none)|\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*
\*\*\*\*\*\*\*\*\*\*| -#### Hints and Guidelines +## Hints and Guidelines ![](/assets/chapter-6-images/01.Rectangle-of-10-x-10-stars-01.png) How does the example work? We initialize **a loop with a variable `i = 1`**, which increases with each iteration of the loop, while it is **less or equal to 10**. This way the code in the body of the loop is executed **10 times**. In the body of the loop we print a new line on the console **`new string('*', 10)`**, which creates a string of 10 stars. -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#0](https://judge.softuni.bg/Contests/Practice/Index/512#0). diff --git a/Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md b/Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md index 246737234..9404070e6 100644 --- a/Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md +++ b/Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md @@ -1,4 +1,4 @@ -### Example: Rectangle Made of N x N Stars +# Example: Rectangle Made of N x N Stars Write a program that gets a positive integer **n** and prints in the console **a rectangle made out of N x N stars**. @@ -6,10 +6,10 @@ Write a program that gets a positive integer **n** and prints in the console **a |---|---|---|---|---|---| |2|\*\*
\*\*|3|\*\*\*
\*\*\*
\*\*\*|4|\*\*\*\*
\*\*\*\*
\*\*\*\*
\*\*\*\*| -#### Hints and Guidelines +## Hints and Guidelines ![](/assets/chapter-6-images/02.Rectangle-of-N-x-N-stars-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#1](https://judge.softuni.bg/Contests/Practice/Index/512#1). diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md index e644179d7..9e28af145 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md @@ -1,6 +1,57 @@ -## Lab: Drawing Figures in a Web Environment +# Lab: Drawing Ratings in Web -Now that we got used to **nested loops** and the way to use them to draw figures on the console, we can get into something even more interesting: we can see how loops can be used to **draw in a web environment**. We will make a web application that visualizes a number rating \(a number from 0 to 100\) with stars. This kind of visualization is common in e-commerce sites, reviews of products, event rating, rating of apps, and others. +Now that we got used to **nested loops** and the way to use them to draw figures on the console, we can get into something even more interesting: we can see how loops can be used to **draw in a Web environment**. We will make a web application that visualizes a number rating (a number from 0 to 100) with stars. This kind of visualization is common in e-commerce sites, reviews of products, event rating, rating of apps, and others. Don't worry if you don't understand all of the code, how exactly it is written and how the project works. It is normal, now we are learning to write code and we are a long way from the web development technologies. If you are struggling to write your project by following the steps, **watch the video** from the beginning of the chapter or ask in the SoftUni forum: [https://softuni.bg/forum](https://softuni.bg/forum). +## Ratings – Visualization in a Web Environment + +Your task now is to create an **ASP.NET MVC Web Application** for visualizing a rating (a number from 0 to 100). 1 to 10 stars should be drawn (with halves). The stars should be generated with a **`for`** loop. + +![](/assets/chapter-6-images/11.Ratings-01.png) + +## Creating a New C# Project + +Create a new ASP.NET MVC web app with C# in Visual Studio. Add a new project from [**Solution Explorer**] -> [**Add**] -> [**New Project…**]. Give it a meaningful name, for example "WebApp-Ratings". + +![](/assets/chapter-6-images/11.Ratings-02.png) + +Choose the type of the app to be **MVC**. + +![](/assets/chapter-6-images/11.Ratings-03.png) + +## Creating a View Holding a HTML Form + +Open and edit the file **`Views/Home/Index.cshtml`**. Delete everything and insert the following code: + +![](/assets/chapter-6-images/11.Ratings-04.png) + +This code creates a web form **`
`** with a field **`"rating"`** for inputting a number in the interval [**0 … 100**] and a button [**Draw**] to send data from the form to the server. The action that will process the data is called **`/Home/DrawRatings`**, which means method **`DrawRatings`** in controller **`Home`**, which is in the file **`HomeController.cs`**. After the form the contents of **`ViewBag.Stars`** are printed. The code that will be inside will be dynamically generated by the HTML controller with a series of stars. + +## Adding the DrawRatings(int) Method + +Add a method **`DrawRatings`** in the controller **`HomeController`**. Open the file **`Controllers/HomeController.cs`** and add the following code: + +![](/assets/chapter-6-images/11.Ratings-05.png) + +The above code takes the number **`rating`**, makes some calculations to find the number of **full stars**, the number of **empty stars** and the number of **half-full stars**, after which it generates an HTML code, which orders a few pictures of stars one after the other so that it can make the rating picture from them. The ready HTML code is stored in **`ViewBag.Stars`** to visualize the view **`Index.cshtml`**. Additionally the sent rating is kept (as a number) in **`ViewBag.Rating`**, so that it can be put in the field for rating in the view. In order to ease your work, you can help yourself with the picture of Visual Studio below: + +![](/assets/chapter-6-images/11.Ratings-06.png) + +## Adding Star Images + +Create a new **images** folder in the project, using [**Solution Explorer**]: + +![](/assets/chapter-6-images/11.Ratings-07.png) + +Now add **the pictures with the stars** (they are a part of the files with this project and can be downloaded [here](https://github.com/SoftUni/Programming-Basics-Book-CSharp-BG/tree/master/assets/chapter-6-assets)). Copy them from Windows Explorer and paste them in the **images** folder in [**Solution Explorer**] in Visual Studio. + +![](/assets/chapter-6-images/11.Ratings-08.png) + +## Starting and Testing the Project + +Start the project with [**Ctrl+F5**] and enjoy: + +![](/assets/chapter-6-images/11.Ratings-09.png) + +If you have a problem with the example project above, **watch the video** in the beginning of the chapter. There the application is made live and step by step with a lot of explanations. Or ask in **the SoftUni forum**: https://softuni.bg/forum. diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/add-star-images.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/add-star-images.md index ac64c14ac..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/add-star-images.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/add-star-images.md @@ -1,9 +0,0 @@ -#### Adding Star Images - -Create a new **images** folder in the project, using [**Solution Explorer**]: - -![](/assets/chapter-6-images/11.Ratings-07.png) - -Now add **the pictures with the stars** (they are a part of the files with this project and can be downloaded [here](https://github.com/SoftUni/Programming-Basics-Book-CSharp-BG/tree/master/assets/chapter-6-assets)). Copy them from Windows Explorer and paste them in the **images** folder in [**Solution Explorer**] in Visual Studio. - -![](/assets/chapter-6-images/11.Ratings-08.png) diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-new-project.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-new-project.md index caf48ebc7..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-new-project.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-new-project.md @@ -1,9 +0,0 @@ -#### Creating a New C# Project - -Create a new ASP.NET MVC web app with C# in Visual Studio. Add a new project from [**Solution Explorer**] -> [**Add**] -> [**New Project…**]. Give it a meaningful name, for example "WebApp-Ratings". - -![](/assets/chapter-6-images/11.Ratings-02.png) - -Choose the type of the app to be **MVC**. - -![](/assets/chapter-6-images/11.Ratings-03.png) diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-web-form.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-web-form.md index c0e848ff3..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-web-form.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-web-form.md @@ -1,7 +0,0 @@ -#### Creating a Web Form - -Open and edit the file **`Views/Home/Index.cshtml`**. Delete everything and insert the following code: - -![](/assets/chapter-6-images/11.Ratings-04.png) - -This code creates a web form **``** with a field **`"rating"`** for inputting a number in the interval [**0 … 100**] and a button [**Draw**] to send data from the form to the server. The action that will process the data is called **`/Home/DrawRatings`**, which means method **`DrawRatings`** in controller **`Home`**, which is in the file **`HomeController.cs`**. After the form the contents of **`ViewBag.Stars`** are printed. The code that will be inside will be dynamically generated by the HTML controller with a series of stars. diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/drawratings-method.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/drawratings-method.md index 5a7ab2882..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/drawratings-method.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/drawratings-method.md @@ -1,9 +0,0 @@ -#### Adding the DrawRatings Method - -Add a method **`DrawRatings`** in the controller **`HomeController`**. Open the file **`Controllers/HomeController.cs`** and add the following code: - -![](/assets/chapter-6-images/11.Ratings-05.png) - -The above code takes the number **`rating`**, makes some calculations to find the number of **full stars**, the number of **empty stars** and the number of **half-full stars**, after which it generates an HTML code, which orders a few pictures of stars one after the other so that it can make the rating picture from them. The ready HTML code is stored in **`ViewBag.Stars`** to visualize the view **`Index.cshtml`**. Additionally the sent rating is kept (as a number) in **`ViewBag.Rating`**, so that it can be put in the field for rating in the view. In order to ease your work, you can help yourself with the picture of Visual Studio below: - -![](/assets/chapter-6-images/11.Ratings-06.png) diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/ratings.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/ratings.md index 206f5a9a5..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/ratings.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/ratings.md @@ -1,5 +0,0 @@ -### Problem: Ratings – Visualization in a Web Environment - -Create an ASP.NET MVC web application for visualizing a rating (a number from 0 to 100). 1 to 10 stars should be drawn (with halves). The stars should be generated with a **`for`** loop. - -![](/assets/chapter-6-images/11.Ratings-01.png) diff --git a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/start-project.md b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/start-project.md index cf8fba1ec..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/start-project.md +++ b/Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/start-project.md @@ -1,7 +0,0 @@ -#### Starting the Project - -Start the project with [**Ctrl+F5**] and enjoy: - -![](/assets/chapter-6-images/11.Ratings-09.png) - -If you have a problem with the example project above, **watch the video** in the beginning of the chapter. There the application is made live and step by step with a lot of explanations. Or ask in **the SoftUni forum**: https://softuni.bg/forum. diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md b/Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md index bc676351a..bb7e93488 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md @@ -1,4 +1,4 @@ -### Example: Christmas Tree +# Problem: Christmas Tree Write a program that takes a number **n** (1 ≤ n ≤ 100) and prints a Christmas tree with height of **n+1**. @@ -10,12 +10,12 @@ Write a program that takes a number **n** (1 ≤ n ≤ 100) and prints a Christm |---|---|---|---| |3|    |    
  \* | \*  
 \*\* | \*\* 
\*\*\* | \*\*\*|4|     |     
   \* | \*   
  \*\* | \*\*  
 \*\*\* | \*\*\* 
\*\*\*\* | \*\*\*\*| -#### Hints and Guidelines +## Hints and Guidelines From the examples we see that **the Christmas tree** can be **divided** into **three** logical parts. **The first** part is **the stars and the white spaces before and after them**, **the middle** part is **` | `**, and **the last** part is again **stars**, but this time there are **white spaces** only **before** them. The printing can be done with only **one loop** and the **`new string(…)`** constructor, which we will use once for the stars and once for the white spaces. ![](/assets/chapter-6-images/07.Christmas-tree-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#6](https://judge.softuni.bg/Contests/Practice/Index/512#6). diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md b/Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md index f94b22e07..ec7f651ef 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md @@ -1,4 +1,4 @@ -### Example: Rhombus Made of Stars +# Problem: Rhombus Made of Stars Write a program that takes a positive integer **n** and prints **a rhombus made of stars** with size **n**. @@ -6,12 +6,11 @@ Write a program that takes a positive integer **n** and prints **a rhombus made |---|---|---|---| |1|\*|2| \* 
\* \*
 \* 
| - |Input|Output|Input|Output| |---|---|---|---| |3|  \*  
 \* \* 
\* \* \*
 \* \* 
  \*  |4|   \*   
  \* \*  
 \* \* \* 
\* \* \* \*
 \* \* \* 
  \* \*  
   \*   | -#### Hints and Guidelines +## Hints and Guidelines To solve this problem we need to mentally **divide** **the rhombus** into **two parts** - **upper** one, which **also** includes the middle row, and **lower** one. For **the printing** of each part we will use **two** separate loops, as we leave the reader to decide the dependency between **`n`** and the variables of the loops. For the first loop we can use the following guidelines: @@ -23,6 +22,6 @@ To solve this problem we need to mentally **divide** **the rhombus** into **two ![](/assets/chapter-6-images/06.Rhombus-of-stars-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#5](https://judge.softuni.bg/Contests/Practice/Index/512#5). diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/example-square-frame.md b/Content/Chapter-6-1-nested-loops/nested-loops/example-square-frame.md index 53ca0a3eb..17fb8ac2e 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/example-square-frame.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/example-square-frame.md @@ -1,4 +1,4 @@ -### Example: Square Frame +# Example: Square Frame Write a program that takes a positive integer **n** and draws on the console **a square frame** with a size of **n \* n**. @@ -10,7 +10,7 @@ Write a program that takes a positive integer **n** and draws on the console **a |---|---|---|---| |5|+ - - - +
| - - - |
| - - - |
| - - - |
+ - - - +|6|+ - - - - +
| - - - - |
| - - - - |
| - - - - |
| - - - - |
+ - - - - +| -#### Hints and Guidelines +## Hints and Guidelines We can solve the problem in the following way: * We read from the console the number **`n`**. @@ -22,6 +22,6 @@ Here is an example implementation of the above idea with nested loops: ![](/assets/chapter-6-images/05.Square-frame-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#4](https://judge.softuni.bg/Contests/Practice/Index/512#4). diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/example-square-of-stars.md b/Content/Chapter-6-1-nested-loops/nested-loops/example-square-of-stars.md index 5e7ac97dd..b1c2da9cc 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/example-square-of-stars.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/example-square-of-stars.md @@ -1,4 +1,4 @@ -### Examples: Square Made of Stars +# Examples: Square Made of Stars Print on the console a square made of **N x N** stars: @@ -6,12 +6,12 @@ Print on the console a square made of **N x N** stars: |---|---|---|---|---|---| |2|\* \*
\* \*|3|\* \* \*
\* \* \*
\* \* \*|4|\* \* \* \*
\* \* \* \*
\* \* \* \*
\* \* \* \*| -#### Hints and Guidelines +## Hints and Guidelines The problem is similar to the last one. The difference here is that we need to figure out how to add a white space after the stars so that there aren't any excess white spaces in the beginning or the end. ![](/assets/chapter-6-images/03.Square-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#2](https://judge.softuni.bg/Contests/Practice/Index/512#2). diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/example-triangle-of-dollars.md b/Content/Chapter-6-1-nested-loops/nested-loops/example-triangle-of-dollars.md index 848677e33..bc3e997d8 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/example-triangle-of-dollars.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/example-triangle-of-dollars.md @@ -1,4 +1,4 @@ -### Example: Triangle Made of Dollars +# Example: Triangle Made of Dollars Write a program that takes the number **n** and prints **a triangle made of dollars**. @@ -6,12 +6,12 @@ Write a program that takes the number **n** and prints **a triangle made of doll |---|---|---|---|---|---| |3|$
$ $
$ $ $|4|$
$ $
$ $ $
$ $ $ $|5|$
$ $
$ $ $
$ $ $ $
$ $ $ $ $| -#### Hints and Guidelines +## Hints and Guidelines The problem is **similar** to those for drawing **a rectangle** and **square**. Once again we will use **nested loops**, but there is **a catch** here. The difference is that **the number of columns** that we need to print depends on **the row**, on which we are and not on the input number **`n`**. From the example input and output data we see that **the count of dollars depends** on which **row** we are on at the moment of the printing, i.e. 1 dollar means first row, 3 dollars mean third row and so on. Let's see the following example in details. We see that **the variable** of **the nested** loop is connected with the variable of **the outer** one. This way our program prints the desired triangle. ![](/assets/chapter-6-images/04.Triangle-of-dollars-01.png) -#### Testing in the Judge System +## Testing in the Judge System Test your solution here: [https://judge.softuni.bg/Contests/Practice/Index/512#3](https://judge.softuni.bg/Contests/Practice/Index/512#3). diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops-example.md b/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops-example.md index d1c044fff..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops-example.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops-example.md @@ -1,7 +0,0 @@ -### Nested Loops - Examples - -Here is an **example** that illustrates nested loops. The aim is again to print a rectangle made of **`n`** * **`n`** stars, in which for each row a loop iterates from **1** to **`n`**, and for each column a nested loop is executed from **1** to **`n`**: - -![](/assets/chapter-6-images/00.Nested-loops-01.png) - -Let's look at the example above. After initializing **the first (outer) loop**, its **body**, which contains **the second (nested) loop** starts executing. By itself it prints on one row **`n`** number of stars. After **the inner** loop **finishes** executing at the first iteration of the outer one, **the first loop will continue**, i.e. it will print an empty row on the console. **After that**, the variable of **the first** loop will be **renewed** and the whole **second** loop will be executed again. The inner loop will execute as many times as the body of the outer loop executes, in this case **`n`** times. diff --git a/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops.md b/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops.md index 1a87b97bb..bd7efe367 100644 --- a/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops.md +++ b/Content/Chapter-6-1-nested-loops/nested-loops/nested-loops.md @@ -1,4 +1,4 @@ -## Nested Loops +# Nested Loops – Concepts Nested loops is a construction where **in the body of one loop** (outer one) **another loop is run** (inner one). In each iteration of the outer loop, **the whole** inner loop is executed. This happens in the following way: @@ -6,3 +6,11 @@ Nested loops is a construction where **in the body of one loop** (outer one) **a - After that, **the inner loop is executed**. The controlling variables start position is initialized, a check for ending the loop is made and the code in its body is executed. - When reaching the the set value for **ending the loop**, the program goes back one step up and continues executing the previous (outer) loop. The controlling variable of the outer loop changes with one step, a check is made to see if the condition for ending the loop is met and **a new execution of the nested (inner) loop is started**. - This is repeated until the variable of the outer loop meets the condition to **end the loop**. + +## Nested Loops – Examples + +Here is an **example** that illustrates nested loops. The aim is again to print a rectangle made of **`n`** * **`n`** stars, in which for each row a loop iterates from **1** to **`n`**, and for each column a nested loop is executed from **1** to **`n`**: + +![](/assets/chapter-6-images/00.Nested-loops-01.png) + +Let's look at the example above. After initializing **the first (outer) loop**, its **body**, which contains **the second (nested) loop** starts executing. By itself it prints on one row **`n`** number of stars. After **the inner** loop **finishes** executing at the first iteration of the outer one, **the first loop will continue**, i.e. it will print an empty row on the console. **After that**, the variable of **the first** loop will be **renewed** and the whole **second** loop will be executed again. The inner loop will execute as many times as the body of the outer loop executes, in this case **`n`** times. diff --git a/Content/Chapter-6-1-nested-loops/what-we-learned/what-we-learned.md b/Content/Chapter-6-1-nested-loops/what-we-learned/what-we-learned.md index 07691f5d1..e69de29bb 100644 --- a/Content/Chapter-6-1-nested-loops/what-we-learned/what-we-learned.md +++ b/Content/Chapter-6-1-nested-loops/what-we-learned/what-we-learned.md @@ -1,19 +0,0 @@ -## What We Learned in This Chapter? - -We became acquainted with the **`new string`** constructor: - -```csharp -string printMe = new string('*', 5); -``` - -We learned to draw figures with nested **`for`** loops: - -```csharp -for (var r = 1; r <= 5; r++) -{ - Console.Write("*"); - for (var c = 1; c < 5; c++) - Console.Write(" *"); - Console.WriteLine(); -} -``` diff --git a/SUMMARY.md b/SUMMARY.md index 0081882d7..4c127fab9 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -132,37 +132,19 @@ * [Problem: Division without Remainder](Content/Chapter-5-2-loops-exam-problems/exam-problems/division/division.md) * [Problem: Logistics](Content/Chapter-5-2-loops-exam-problems/exam-problems/logistics/logistics.md) * [6.1. Nested Loops](Content/Chapter-6-1-nested-loops/overview.md) - * [Example: Rectangle Made Out of 10 x 10 Stars](Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md) - * [Example: Rectangle Made Out of N x N Stars](Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md) * [Nested Loops](Content/Chapter-6-1-nested-loops/nested-loops/nested-loops.md) - * [Nested Loops - Examples](Content/Chapter-6-1-nested-loops/nested-loops/nested-loops-example.md) + * [Example: Rectangle Made Out of 10 x 10 Stars](Content/Chapter-6-1-nested-loops/example-rectangle-10x10/example-rectangle-10x10.md) + * [Example: Rectangle Made Out of N x N Stars](Content/Chapter-6-1-nested-loops/example-rectangle-NxN/example-rectangle-NxN.md) * [Examples: Square Made Out of Stars](Content/Chapter-6-1-nested-loops/nested-loops/example-square-of-stars.md) * [Example: Triangle Made Out of Dollars](Content/Chapter-6-1-nested-loops/nested-loops/example-triangle-of-dollars.md) * [Example: Square Frame](Content/Chapter-6-1-nested-loops/nested-loops/example-square-frame.md) - * [Example: Rhombus Made Out of Stars](Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md) - * [Example: Christmas Tree](Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md) - * [Drawing More Complex Figures](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md) - * [Example: Sunglasses](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md) - * [Printing the Top and Bottom Row](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-top-and-bot-row.md) - * [Printing the Middle Rows](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/print-middle-rows.md) - * [Example: House](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md) - * [Tips and Tricks](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md) - * [Reading the Input Data](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/read-input-data.md) - * [Calculating Roof Length](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md) - * [Printing the Roof](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-roof.md) - * [Printing the Base](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md) - * [Example: Diamond](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md) - * [Tips and Tricks](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md) - * [Reading the Input Data and Printing the Top Part of the Diamond](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md) - * [Printing the Bottom Part of the Diamond](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md) - * [What We Learned in This Chapter?](Content/Chapter-6-1-nested-loops/what-we-learned/what-we-learned.md) - * [Lab: Drawing Figures in a Web Environment](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md) - * [Problem: Ratings – Visualization in a Web Environment](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/ratings.md) - * [Creating a New C\# Project](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-new-project.md) - * [Creating a Web Form](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/create-web-form.md) - * [Adding the DrawRatings Method](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/drawratings-method.md) - * [Adding Star Images](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/add-star-images.md) - * [Starting the Project](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/ratings/start-project.md) + * [Exercises: Drawing Figures](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md) + * [Problem: Rhombus of Stars](Content/Chapter-6-1-nested-loops/nested-loops/example-rhombus-of-stars.md) + * [Problem: Christmas Tree](Content/Chapter-6-1-nested-loops/nested-loops/example-christmas-tree.md) + * [Problem: Sunglasses](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/sunglasses/sunglasses.md) + * [Problem: House](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/house.md) + * [Problem: Diamond](Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/diamond.md) + * [Lab: Drawing Ratings in a Web](Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-web-app/exercise-drawing-figures-in-web-app.md) * [6.2. Nested Loops - Exam Problems](Content/Chapter-6-2-nested-loops-exam-problems/overview.md) * [Problem: Drawing a Fort](Content/Chapter-6-2-nested-loops-exam-problems/exam-problems/draw-fort/draw-fort.md) * [Problem: Butterfly](Content/Chapter-6-2-nested-loops-exam-problems/exam-problems/butterfly/butterfly.md)