Thread: Need help with spacing

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Need help with spacing

    I have a program that prints out pascal's triangle. One problem: it isn't a triangle. The output doesn't work. This is what it should print:
    Code:
    How many rows: 4
                     1
                  1     1
               1     2     1
            1     3     3     1
         1     4     6     4     1
    and this is what it does print:
    Code:
    Enter a number of rows: 4
                1
                1           1
                1           2            1
                1           3            3            1
                1           4            6            4            1
    Can someone help me get the spacing right? My code is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int combinations (int n, int k) {
        if (k == 0 || k == n) {
            return 1;
        }
        else {
            return combinations(n-1,k-1) + combinations(n-1,k);
        }
    }
    int main ( ) {
        int rows;
        cout << "Enter a number of rows: ";
        cin >> rows;
        for(int r = 0; r < rows+1; r++) {
                cout << "            " << "1";
            for(int c = 1; c < r+1; c++) {
                         
                cout << "           " << combinations(r, c) << ' ';
                
            }
            cout << endl;
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    any help?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    Code:
    cout << "           " << "1";
    This is your code for outputting the initial spaces. So it should be clear why the code is doing what it's doing now. It just outputs 12 spaces every time.

    The question is, how can you figure out how many spaces to put for each row? You should first ask yourself how you'd do it if you were drawing the pyramid yourself. How many spaces should be in the beginning of the first row if the user inputs 4? What if the user inputs 2? What if they input 1? Can you find a pattern? If you can find a pattern, you can then try to put that pattern into code.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I almost have it now, it just prints too many spaces to the left.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int combinations (int n, int k) {
        if (k == 0 || k == n) {
            return 1;
        }
        else {
            return combinations(n-1,k-1) + combinations(n-1,k);
        }
    }
    int main ( ) {
        int rows;
        cout << "Enter a number of rows: ";
        cin >> rows;
        for(int r = 0; r < rows+1; r++) {
              cout << setw(3 * (rows - r) + 1) << '1';                
            for(int c = 1; c < r+1; c++) {
                cout << setw(6) << combinations(r, c);
                
            }
            cout << endl;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev C++ Spacing Problem
    By Darklighter137 in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2006, 10:48 AM
  2. Spacing?
    By trenzterra in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:42 PM
  3. Back Spacing
    By vasanth in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2002, 11:15 AM
  4. Help with SPACING
    By cprogstudent in forum C++ Programming
    Replies: 11
    Last Post: 04-24-2002, 05:36 AM
  5. Spacing out!!!!
    By Olland in forum C Programming
    Replies: 4
    Last Post: 01-25-2002, 04:02 AM