C++ inverted trapezoid dispay using nested for loops

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:
1 *******
2 *****
3 ***
(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:

for (i = nrows; i >= 1; i--)
{


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

The ouput is just blank as of now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main ()
{
int a=7,b=3;//use this as input
    for(int i=0;i<b;i++)
    {
        for(int x=a-i*2;x>0;x--)
        cout<<"*";
        cout<<endl;
    }
    return 0;
}
closed account (i8T4izwU)
The 7 was just an example. The user inputs the number of asterisk In the top row and the number of rows. So I don't think I'm going to be able to assign destination values like 7 and 3 to a. I think I have to is one for loop for number of blank spaces and another for loop for number of asterisk. That's the problem I'm having.
Last edited on
Topic archived. No new replies allowed.