Thread: How to draw shapes with only for loops?

1. How to draw shapes with only for loops?

Ok I have this problem.

First I need to make a right triangle:

#
##
###

Done.

Then I need to make an opposite right triangle:

- #
-##
###

(I hope you know what I mean. Not any isosceles triangle, just the same triangle but the opposite.)

Not done.

Here is the code I used for the first one:

Code:
```	for (int row = 0; row < 10; row++)
{
for (int col = 0; col <= row; col++)
{
cout << "#";
}
cout << "\n";
}```
For the second one my code was this, but the problem is that I can't use the int count = row - 2, and the if statement that goes along with it. I can only use another for loop.

Code:
```for (int row = 0; row > 3; row++)
{
int count = row - 2;
for (int col = 0; col > 3; col++)
{
count ++;
if ( count - 1 == 0 )
{
cout << "#";
}
else {cout << " ";}
}
cout << "\n";
}```
Can anyone help me with this? I need another for loop added to the original code for the first right triangle.

2. So you need a loop to print the spaces (and you already calculated how many spaces you need, it looks like).

3. Yea. Right now what I would like to know is how do I make the right triangle flip upside down? So that there will be 9 '-" on the first line, 8 on the second, 7 on the third, etc. Then at the end of each line after all the '-', there will be a '#', so on the first line there will be 1 of them, then 2, then 3 etc.

So that way I can make my opposite right triangle.

4. See if you take this code and run it you will see the dashes and the #'s. I need to know how to flip the dashes upside down, and the #'s flip them to the side, so the #'s will form my opposite right triangle.

Code:
```for (int row = 0; row < 10; row++)
{
for (int col = 0; col <= row; col++)
{
cout << "-";
}

for (int lol = 0; lol < row + 1; lol++)
{
cout << "#";
}

cout << "\n";
}```

5. Code:
```for (int row = 0; row < 10; row++)
{
for (int col = 0; col <= row - 1; col++)
{
cout << "-";
}

for (int lol = 0; lol <= 9 - row; lol++)
{
cout << "#";
}

cout << "\n";
}```
Ok great this one is great, except I need the whole output to be upside down. How do I do that? =/

6. Originally Posted by CplusplusNewb
See if you take this code and run it you will see the dashes and the #'s. I need to know how to flip the dashes upside down, and the #'s flip them to the side, so the #'s will form my opposite right triangle.
The answer to that is to think for seven seconds. In the first row, how many dashes do you want to print? 9? Then why oh why stop your loop at 0? That doesn't make any sense.

7. Well I'm only a beginner, what you jsut said will actually take me like 5 trial and errors. >.>

8. Code:
```for (int row = 9; row < 10; row++)
{
for (int col = 0; col <= row - 1; col++)
{
cout << "-";
}

for (int lol = 0; lol <= 9 - row; lol++)
{
cout << "#";
}

cout << "\n";
}```
Ok for this one it outputs

---------#

9 dashes and 1 #.

What should I change so that as the number of dashes decreases by 1, the number of #'s increases by 1?

9. Originally Posted by CplusplusNewb
[code]

What should I change so that as the number of dashes decreases by 1, the number of #'s increases by 1?
Nothing? That will happen as your loop continues all by itself, once you allow your loop to loop (currently it goes from 9 to 9 which isn't much of a loop).

10. Ok what I don't understand is at what number my loop starts and at what number it ends (in the code).

When I change the first number to 0 my output is:

10 #'s
1 dash and 9 #'s

..
..
..

9 dashes 1 #.

That's the opposite of what I want it to do. I want it to start with 9 dashes and 1 # and end with 10 #'s.

11. Originally Posted by CplusplusNewb
Ok what I don't understand is at what number my loop starts and at what number it ends (in the code).

When I change the first number to 0 my output is:

10 #'s
1 dash and 9 #'s

..
..
..

9 dashes 1 #.

That's the opposite of what I want it to do. I want it to start with 9 dashes and 1 # and end with 10 #'s.
So go the other way.

12. Well now I'm completely stumped. Can you please tell me which numbers I switch? I'm really confused.

13. Code:
```for (int row = 0; row < 10; row++)
{
for (int col = 0; col <= row; col++)
{
cout << "-";
}

for (int lol = 0; lol <= 8 - row; lol++)
{
cout << "#";
}

cout << "\n";
}```
Can you please tell me which line I have to make changes to in order to flip the output upside down?

14. you could use a 2d array for it, you need to be able to store and check whether it is a "#" or a "-", and you can't do that with cout and cin(?). here's some code below, as you can see you have to set the values of the array before you use it otherwise you'd just have a block of 0's (you could also set these by reading values from a file)
Code:
```int screen[10][8] = {1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1}; //[row][column] 10 down eight across

for (int row = 0; row < 9; row++){
for (int column = 0; column < 7; column++){
if (screen[row][column] == 1){
cout << "#";
}
else{
cout << "-";
}
}
cout << "\n";
}
cout << "\n";
//flip  # into - and vice versa
for (int row = 1; row < 10; row++){ // for some reason 0 and 9 was making this get offset by one line? however this should make it go out of the array... but it doesn't...
for (int column = 7; column > 0; column--){//draw last column first and first column last, thus flipping the image horizontally
if (screen[row][column] == 1){
cout << "#";
}
else{
cout << "-";
}
}
cout << "\n";
}
cout << "\n";```

15. Originally Posted by robolee
use a 2d array for it, you need to be able to store and check whether it is a "#" or a "-", and you can't do that with cout and cin(?). here's the code below, but you'd have to set the values of the array beforehand (read from a file?)
Code:
```screen[10][8]; //[row][column]

for (int row = 0; row < 9; row++){
for (int col = 0; col <= 7; col++){
if (screen[row][column] == 1){
cout << "#";
}
else{
cout << "-";
}
}
}

//flip  # into - and vice versa
for (int row = 0; row < 9; row++){
for (int col = 0; col <= 7; col++){
if (screen[row][column] == 1){
screen[row][column] == 0;
}
else{
screen[row][column] == 1;
}
}
}```
I'd just like to flag the above as bad advice. It doesn't help at all; rather it needlessly complicates things. It amounts to an attempt to throw the kitchen sink at the problem. You probably haven't learnt arrays yet, and certainly should not involve file IO for such a simple problem. It is also clearly very buggy and untested code.

To invert the output you need to reverse the row numbers. Remember that rows are the horizontal strips? We want to display those strips in the reverse order. Currently it goes form 0 to 9 and you need it to go from 9 down to zero.