# Making a pyramid of Xs

• 12-11-2003
Tride
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.
• 12-11-2003
Prelude
>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.
• 12-11-2003
SLICK
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.
• 12-11-2003
glUser3f
you mean something like this:
Code:

```  X  XXX  XXXXX```
right?

use [ code ] tags [ /code ] when posting something that requires ident.
• 12-11-2003
Tride
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.

Quote:

use [ code ] tags [ /code ] when posting something that requires ident.
Thanks. I fixed it ;)
• 12-11-2003
jlou
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.
• 12-12-2003
JasonD
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.
• 12-12-2003
Tride
Thanks guys :). That's much simpler than I was expecting.
• 12-12-2003
Prelude
>That's much simpler than I was expecting.
It usually is. Remember that when you face a seemingly improbable problem. :)