Back learning after silly life issues derailed me! I decided to switch my learning material and I'm now working through Accelerated C++.
Chapter 2, Exercise 5:
Write a set of "*" characters so that they form a square, a rectangle, and a triangle.
I tried but just couldn't get the triangle down exactly. A quick google found the following answer:
// draw triangle
int row = 0;
int col = 0;
int height = 5;
// draw rows above base
while (row < height - 1)
{
col = 0;
while (col < height + row)
{
++col;
if (col == height - row)
cout << '*';
else
{
if (col == height + row)
cout << '*';
else
cout << ' ';
}
}
cout << endl;
++row;
}
// draw the base
col = 0;
while (col < height * 2 - 1)
{
cout << '*';
++col;
}
I wanted to disect this and fully understand it as I had trouble coming up with my own answer. It doesn't matter how many times I go through it I cannot see how it's drawing the right side of the triangle:
- - - - *
- - - *
- - *
- *
*
* * * * * * * * * *
That's what I get going through this loop on paper. Where on earth is that right side coming from? I have a gut feeling the expressions are doing something I'm not seeing. The code works.