-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates Content/Chapter-6-1-nested-loops/exercise-drawing-figures-in-…
…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
Showing
32 changed files
with
312 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
...ent/Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/guidelines.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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**). | ||
15 changes: 0 additions & 15 deletions
15
...Chapter-6-1-nested-loops/drawing-more-complex-figures/diamond/print-bot-part.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
29 changes: 0 additions & 29 deletions
29
...oops/drawing-more-complex-figures/diamond/read-input-data-and-print-top-part.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
36 changes: 34 additions & 2 deletions
36
...r-6-1-nested-loops/drawing-more-complex-figures/drawing-more-complex-figures.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
23 changes: 0 additions & 23 deletions
23
...Chapter-6-1-nested-loops/drawing-more-complex-figures/house/calc-roof-length.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`**. | ||
21 changes: 0 additions & 21 deletions
21
Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/guidelines.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`**. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
Content/Chapter-6-1-nested-loops/drawing-more-complex-figures/house/print-base.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
Oops, something went wrong.