Thread: Making a pyramid of Xs

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    Making a pyramid of Xs

    My book says to, as an excercise, make a pyramid of Xs 20 lines long, like this:
    Code:
                                                    X
                                                   XXX
                                                  XXXXX
                                                 XXXXXXX
    And so on.

    After trying to do it many times, the most I could do was make lines of Xs that increased by 1 every line (the pyramid requires the Xs to be increased by two). Can someone give me the code to do this? My book says to use 3 for loops, which I've been trying to do. I just need to study the code needed for this program carefully.
    Last edited by Tride; 12-11-2003 at 04:48 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can someone give me the code to do this?
    No, do it yourself.

    >My book says to use 3 for loops, which I've been trying to do.
    Code:
    loop1: for each row
      loop2: Print n spaces
      loop3: print n X's
    Notice how loops two and three are nested inside of loop one. Once you get that it's just a matter of using three variables: One for the rows, one for the number of spaces (which counts down), and one for the number of X's (which counts up, matching how the number of spaces counts down).

    >I just need to study the code needed for this program carefully.
    Since you've not shown that you've tried anything, we have no choice but to assume that you will simply turn in code that we give you without looking at it. If this is true or not is irrelevant, it's just the way things work around here.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Making a pyramid of Xs

    Making a pyramid of Xs
    My book says to, as an excercise, make a pyramid of Xs 20 lines long, like this:

    X
    XXX
    XXXXX
    XXXXXXX
    And so on.

    After trying to do it many times, the most I could do was make lines of Xs that increased by 1 every line (the pyramid requires the Xs to be increased by two). Can someone give me the code to do this? My book says to use 3 for loops, which I've been trying to do. I just need to study the code needed for this program carefully.


    > Post the code that you have and we can better help you.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    you mean something like this:
    Code:
      X
     XXX  
    XXXXX
    right?

    use [ code ] tags [ /code ] when posting something that requires ident.
    Last edited by glUser3f; 12-11-2003 at 01:30 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    49
    Here is what I already have:

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	int rows;
        int x;
    	int space;
    
    	for (rows = 0; rows <= 5; rows++)
    	{
    		for (space = 5; space >= rows; space--)
    		{
    			cout << " ";
    		}
    
    		for (x = 0; x <= rows; x++)
    			cout << "X";
    		    cout << endl;
    	}
    
        
        return 0;
    }
    I can't seem to add two more Xs to each line, and I'm sure there is more wrong with the code than that.

    use [ code ] tags [ /code ] when posting something that requires ident.
    Thanks. I fixed it

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Ask yourself, what is the relationship between the line number and the number of X's?

    Line 0: 1 X
    Line 1: 3 X's
    Line 2: 5 X's
    Line 3: 7 X's
    .
    .
    .
    Line 19: 39 X's

    It's a pretty simple equation. Once you figure out the equation, use it in the code that outputs the X's.

  7. #7
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Obviously, using the row number within the X drawing loop is only going to increase the number X's drawn for each row by one. You need to use some other value.

    You could use a formula, as jlou has stated, or you could set a variable to be the number of X's on the first line. And increment this variable by the number of additional X's for each next line. Use this variable, instead of the row number to determine the number of X's to print.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    49
    Thanks guys . That's much simpler than I was expecting.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That's much simpler than I was expecting.
    It usually is. Remember that when you face a seemingly improbable problem.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. Creating a half pyramid
    By rightbrainer in forum C Programming
    Replies: 7
    Last Post: 04-21-2009, 01:29 AM
  3. C++ Cannonball Pyramid Calculator (tetrahedron)
    By Zedaine in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 04:52 AM
  4. Making a pyramid using recursive functions
    By MikeBahu in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2007, 11:58 PM
  5. Making a pyramid of stars help
    By g1bber in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2006, 08:03 PM