Help displaying inverted pyramid

closed account (i8T4izwU)
The user will enter the number of '*'s on the 1st row (ntop) and then the number of rows forming the trapezoid (nrows). (using <iostream>, cout)

For instance, an inverted trapezoid with 7 '*"s in the 1st row and 3 rows forming the
inverted trapezoid looks like:

*******
 *****
  ***

(this pyramid is centered, in case it isnt when its posted). Also, each descending row has two less asteriks than the above row.

im having trouble with the four loop displaying the number of "*" and " ". I know its a relationship with variables in the for loops, my output is just never doing what i want it to.

THis is the guideline for the for loop:
Use for loops to display the inverted trapezoid. Your outer for loop will iterate
the total number of rows times. For each row use one nested for loop to display
blanks (the 1st row contains no blanks) and another nested for loop to display
the characters '*'.


Heres my for loops so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
for (i = nrows; i >= 1; i--)
{


for (j = 0; j >= nrows; j++) 
{
cout << " ";
}
for (k=ntop; k >= 2; k--)
{
cout << "*";
}
}


The output is just blank as of now.
Last edited on
Line 5: The end condition; I bet 0 is not as big as nrows.

Line 9: Who is ntop?
closed account (i8T4izwU)
Ntop is the number of asterisk in the first row. The user enters that and the number of rows. So if someone enters 9 for ntop and two for number of rows, there will be 9 asterisks in the first row and 7 in the second. I was thinking you could start at ntop and just subtract two each time but I'm having trouble incorporating an equation with the number if rows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

int main()
{
    int ntop=0,nrows=0;
    cout<<"Enter top: ";
    cin>>ntop;
    cout<<"Enter rows: ";
    cin>>nrows;
    for(int x=0;x<nrows;x++)
    {
        for(int y=ntop-(x*2);y>0;y--)
            cout<<"*";
            cout<<endl;
                for(int i=0;i<x+1;i++)
                    cout<<" ";
    }
    return 0;
}
Topic archived. No new replies allowed.