Skip to content

Commit

Permalink
Updates Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-…
Browse files Browse the repository at this point in the history
…web-app/ratings/start-project.md

Auto commit by GitBook Editor
  • Loading branch information
Svetlin Nakov committed Jan 24, 2019
1 parent 3e284e6 commit 4503c80
Show file tree
Hide file tree
Showing 32 changed files with 312 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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|<code>---\*---</code><br><code>--\*-\*--</code><br><code>-\*---\*-</code><br><code>\*-----\*</code><br><code>-\*---\*-</code><br><code>--\*-\*--</code><br><code>---\*---</code><br>|8|<code>---\*\*---</code><br><code>--\*--\*--</code><br><code>-\*----\*-</code><br><code>\*------\*</code><br><code>-\*----\*-</code><br><code>--\*--\*--</code><br><code>---\*\*---</code><br>|9|<code>----\*----</code><br><code>---\*-\*---</code><br><code>--\*---\*--</code><br><code>-\*-----\*-</code><br><code>\*-------\*</code><br><code>-\*-----\*-</code><br><code>--\*---\*--</code><br><code>---\*-\*---</code><br><code>----\*----</code>|

## 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)

<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
<td><b>Repeating a code is considered bad practice</b>, 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 <b>methods</b>. You can look for additional information for methods in the Internet or to look at <a href="chapter-10-methods.md">Chapter “10” (Methods)</a>.</td>
</tr></table>

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).
Original file line number Diff line number Diff line change
@@ -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**).
Original file line number Diff line number Diff line change
@@ -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)

<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
<td><b>Repeating a code is considered bad practice</b>, 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 <b>methods</b>. You can look for additional information for methods in the Internet or to look at <a href="chapter-10-methods.md">Chapter “10” (Methods)</a>.</td>
</tr></table>

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).
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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`**.
Original file line number Diff line number Diff line change
@@ -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`**.
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -9,3 +9,94 @@ Write a program that takes a number **n** (2 ≤ **n** ≤ 100) and prints **a h
|Input|Output|Input|Output|
|---|---|---|---|
|5|<code>--\*--</code><br><code>-\*\*\*-</code><br><code>\*\*\*\*\*</code><br><code>&#124;\*\*\*&#124;</code><br><code>&#124;\*\*\*&#124;</code>|8|<code>---\*\*---</code><br><code>--\*\*\*\*--</code><br><code>-\*\*\*\*\*\*-</code><br><code>\*\*\*\*\*\*\*\*</code><br><code>&#124;\*\*\*\*\*\*&#124;</code><br><code>&#124;\*\*\*\*\*\*&#124;</code><br><code>&#124;\*\*\*\*\*\*&#124;</code><br><code>&#124;\*\*\*\*\*\*&#124;</code><br>|

## 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)

<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
<td><b>It is very important to check if the input data is correct!</b> In these tasks it is not a problem to directly convert the data from the console into <b><code>int</code></b> 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?</td>
</tr></table>

### 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)

<table><tr><td><img src="/assets/alert-icon.png" style="max-width:50px" /></td>
<td>It is not a good idea to add many character strings as it is shown above, because this leads to <strong>performance issues</strong>. Learn more at: <a href="https://bg.wikipedia.org/wiki/%D0%9D%D0%B8%D0%B7#String_Builder">https://bg.wikipedia.org/wiki/%D0%9D%D0%B8%D0%B7#String_Builder</a></td>
</tr></table>

### 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).
Original file line number Diff line number Diff line change
@@ -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).
Loading

0 comments on commit 4503c80

Please sign in to comment.