Thread: How to draw shapes with only for loops?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,336
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  6. #6
    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. >.>

  7. #7
    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?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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).

  9. #9
    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.

  10. #10
    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.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CplusplusNewb View Post
    Well now I'm completely stumped. Can you please tell me which numbers I switch? I'm really confused.
    If you want to change going from 0 to 9 into going from 9 to 0, then perhaps you should switch 0 and 9.

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, 07:11 PM
  2. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11: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