displaying pyramid

Thanks. I got it, fixed.
Last edited on

1
2
3
4
5
6
7
8
9
10
int i, j;
for(i = 1; i <= 9; i++)
{
    for(j = 1; j <= (9 - i); j++)
    cout<<" ";
    for(j = 1; j <= i; j++)
    cout<<setw(1)<<j;
    for(j = (i - 1); j >= 1; j--)
    cout<<setw(1)<<j;
}
Last edited on
your program does not run.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {

    for(int x=1; x<=9; x++) {
        for(int c=1; c<=9-x; c++) cout << ' ';
        int i=x, T=-1;
        for(int y=1; y<=x*2-1; y++) {
            cout << i;
            if(i==1) T*=-1;
            i+=T;
        }
        cout << endl;
    }
    return 0;
}
Done.
Last edited on
i intentionally did it to be a fix size, for you to work the rest out..

it's very easy to modify it from there, come on try it..
Think about the logic.

- You need to draw X rows, where X is the number input by the user.
- Each row has columns which count down from X to 1, then count back up to X
- Each column contains either a number, or spaces
-- ask yourself: what determines whether or not the column contains a space or a number?

Here's a push to get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for( rows )
{
  for( count_down_columns )
  {
    if( I_print_a_number_here )
      print_a_number;
    else
      print_some_spaces;
  }
  for( count_up_columns )
  {
    ...
  }
}



EDIT:

or yeah you could do what blackcoder was suggesting. His is probably a little more efficient actually. Heh.
Last edited on
Thanks.
Last edited on
can post a better code Disch? i'd love to see something more efficient?

note: im not starting a competition, just want to see your style.. how others solve it..
ok you want your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

    //miss spelled here
int main() { 
    int usernum; // inputted from user size of pyramid
// error checking-value must be from 1-15
    do {
        cout << "Enter an integer from 1-15: ";
        cin >> usernum;
    } while (usernum < 1 || usernum > 15);
    for (int rownum = 1; rownum <= usernum; rownum ++ ) {
        for (int spacenum = 1; spacenum <= (usernum - rownum)* 2; spacenum ++)
            cout << " ";
        for (int colnum1 = rownum; colnum1 >= 1; colnum1 --) { //<- adds a bracket
            cout << colnum1 << " ";
        if (colnum1 < 10)
            cout << " ";
        } //<- adds a bracket
                                                //miss spelled here
        for (int colnum2 = 2; colnum2 <= rownum; colnum2 ++)
            cout << colnum2 << " ";
        cout <<endl;
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.