How to draw shapes with only for loops?

This is a discussion on How to draw shapes with only for loops? within the C++ Programming forums, part of the General Programming Boards category; Ok I have this problem. First I need to make a right triangle: # ## ### Done. Then I need ...

  1. #1
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you need a loop to print the spaces (and you already calculated how many spaces you need, it looks like).

  3. #3
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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. #4
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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. #5
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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? =/
    Last edited by CplusplusNewb; 10-16-2009 at 10:16 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CplusplusNewb View Post
    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. #7
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    Well I'm only a beginner, what you jsut said will actually take me like 5 trial and errors. >.>

  8. #8
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CplusplusNewb View Post
    [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. #10
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CplusplusNewb View Post
    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. #12
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    Well now I'm completely stumped. Can you please tell me which numbers I switch? I'm really confused.

  13. #13
    Registered User CplusplusNewb's Avatar
    Join Date
    Oct 2009
    Location
    Chicago
    Posts
    17
    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. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    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
    
    //"draw" your triangle
    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";
    Last edited by robolee; 10-17-2009 at 04:08 PM.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by robolee View Post
    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]
    
    //"draw" your triangle
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to draw shapes and output?
    By Diablo02 in forum C# Programming
    Replies: 3
    Last Post: 11-20-2007, 06:11 PM
  2. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 10:56 PM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  5. Draw Shapes.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-19-2002, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21