Asterisk Pyramid Confusion

I'm trying to write a program that asks the user how many rows in a pyramid he/she wants and it will print a pyramid of asterisks. It is supposed to be a while loop. It is my teacher's first time teaching and he is super confusing...and I can't understand what he says because he is not from the US.

Keep in mind this is basic c++ so it has to be simple.

i figured I would have to have 3 variables (i,j,k) and one would be number of rows, one would be spaces, and one would be asterisks. I know how to ask the user with the cout and cin but I am so stuck on the while loop!!!

I have figured this much out but I need it to be a while loop instead of a for loop.



#include<iostream>
using namespace std;

int main(){
int t, y, x, z;

cout<<"ENTER THE NUMBER OF ROWS: ";
cin>>t;

for(y=1;y<=t;y++){
for (x=t;x>=y;x--){
cout<<" ";
}
for(z=1;z<=y;z++){
cout<<"* ";
}

cout<<endl;
}
}



and instead of printing (if 3 is the desired amount of rows)

--*
-* *
* * *

it needs to print

--*
-***
*****

so it adds two to every line below it
Last edited on
put your code inside [ code ] tags when posting it on the forums please.

also it would be wise to name your variables similar to what they represent, this will help you to visualize your answers.

x,y,z,t are terrible names for variables when they represent words such as row and column.

int maxRows
int currentRow

would be wiser.

1
2
3
4
5
while(currentRow < maxRows)
{
   code here
   ready for a new row? ++currentRow
}


The amount of white around your asteriks will always equal the amount of asteriks in your last row minus the amount of asteriks in your current row.



Row 1 : 4 white spaces asteriks 4 white spaces
Row 2 : 2 white spaces asteriks 2 white spaces
Row 3 : nothing but asteriks.

How to determine number of asteriks in current row?

 
asteriks = (currentRow*2) - 1


and how to determine number of asteriks in last row?
 
maxAsteriks = (maxRows*2) - 1


I think thats somewhat right - if it helps you at all cool if it confuses you then disregard this post. In between classes right now so I'm just browsing the site :P
Last edited on
Topic archived. No new replies allowed.