http://pastebin.com/WBvMuQvH

my code has become somewhat of a mess from trying to move things around and figure out what isn't working.
Basically, what i want it to do is, say the user enters 3, i want it to print out:

1
212
32123

Could you guys give me some help? i feel like I'm close but I'm just missing one small but important step

edit: I'm not sure how formatting works on here but that should be 2 spaces on the first line, then the 1, 1 space on 2nd line, then the 2, and no spaces on the 3rd line

Recommended Answers

All 5 Replies

You can do it by using a recursive procedure with two for loops, first one will be in reverse order and concatinate the decremented/incremented value to a string variable.

#include <iostream>
#include <iomanip>

using namespace std;

void piramid(int count){
    setfill(" ");
    for(int i=0; i<count; i++){
        cout << setw(count-i) << (i+1);
        for(int j=i; j>0; j--){ cout << j; }
        for(int j=1; j<=i; j++){ cout << (j+1); }
        cout << "\n";
        }
    cout << "\n";
    }


int main(){
    piramid(1);
    piramid(2);
    piramid(4);
    piramid(6);
    piramid(9);
    return 0;
    }

Thanks for the help, I think ive got it now.

Ok, so i forgot that i need a space between each number and its really messing with the formatting, I have no idea what's going wrong here

http://pastebin.com/zLxknYYC

void pyramid(int count){
    cin >> lineTotal;
    count = lineTotal;
    for(int line=0; line < count; line++){
        if (count >= 10){
            int width=(count-line)*2;
            if (line<9) width--;
            cout << setw( width ) << line+1;
            }
        else {
            cout << setw(count-line) << (line+1);
            }

        for(int leftside = line; leftside > 0; leftside--){
            cout << setw(count>9&&leftside!=9?2:1) << leftside;
            }
        for(int rightside = 1; rightside <= line; rightside++){
            cout << setw(count>9?2:1) << (rightside + 1);
            }
        cout << endl;
        }
    cout << endl;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.