Thread: Simple loop

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    17

    Simple loop

    Hello all, i want to display an equilateral triangle.

    I thought about it and tried all day but unfortunately didn't do it.
    This is so for i've done.

    Code:
    for(int i=0; i<5; i++)
    {
    for(int j=5; j>i; j--)
     
      for(int k=0; k<i; k++)
        cout<<"*";
        cout<<" ";
    
        cout<<"\n";
    }
    Please let me know where i'm wrong in this loop.

    Thanks in advance..
    Last edited by Aash; 09-18-2010 at 10:11 PM.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    You should give all the for loops curly brackets, even if you don't need to. It makes the code easier to read and helps reduce mistakes.

    Also, I think you should be able to do it with only 2 for loops, as it is a 2d shape after all.
    Last edited by rocketman50; 09-18-2010 at 10:29 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    17
    Firstly sorry to everyone, i've posted it twice. There was something wrong with my internet.

    @rocketman50: Could you help me out please, I tried it so many times.
    I'm using three loops becuz i want to make spaces from left too.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Ah I see, you want spaces from the left.

    You are very almost there. An easy way to modify your code is to do the following.

    First, sort out the brackets

    Code:
    	for(int i=0; i<5; i++){
    		for(int j=5; j>i; j--){
    			cout << " ";
    		}
    
    		for(int k=0; k<i; k++){
    			 cout<<"*";
    		}
        cout<<"\n";
    }
    Then you need to worry about the spacing in-between the stars. A simple IF statement would do. - i.e. if it is the not the first star on the line, add a space before printing a star. So when K is not 0, print out a space before a star.
    Last edited by rocketman50; 09-18-2010 at 11:02 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    17
    I want to get an equilateral triangle , how to do this??

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I find it helps to write a series of comments describing what the function will do, then fill in the code.

    __*
    _***
    *****

    Code:
    //start at 1 and loop until equal to the number of lines in the triangle
    
         //determine the number of spaces required
         spaces = triangle - line 
         //determine the number of *
         asterisks = ( line * 2 ) - 1
    
         //create the string to write
         //for each space
              //add a ' ' char to the string
         //for each astarisk
             //add an '*' to the string
    
         //display the string
    //end
    Of course this will not work unless you use a fixed pitch font...
    Last edited by novacain; 09-19-2010 at 01:47 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having problem with a simple loop
    By everyone0 in forum C Programming
    Replies: 6
    Last Post: 05-28-2010, 05:22 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM