Thread: Pascal's triangle

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

    Pascal's triangle

    I need some help with a program that prints out Pascal's triangle; it just doesn't work. For example, with input 4 it should print:How many rows: 4 1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    but instead it prints:
    Code:
    How many rows? 4
    
    
    
    
      4
     66
    444
    how do I get it to print the right thing? 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 << "How many rows? ";
        cin >> rows;
        int track = rows;
        cout << endl;
        for(int r = 0; r < rows; r++) {
                track--;
            int spaces = track;
    
    
            
            cout << setw(spaces+1);
           for(int amount = 0; amount < r; amount++) {
            cout << combinations(rows, r);
            }
            cout << endl;
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You need to step through your code and realize what it's actually doing. Your problem starts right when this loop first tries to execute.
    Code:
    		for (int amount = 0; amount < r; amount++) {
    			cout << combinations(rows, r);
    		}
    r comes from the outer loop and is initialized to zero, so you end up comparing amount < r or 0 < 0 which is false, so nothing happens.
    Then you iterate and r becomes 1.
    You enter combinations and the effect looks like this:
    Code:
    combinations(rows, r);
      combinations(4, 1)
         return combinations(4 - 1, 1 - 1) + combinations(4 - 1, 1)
           return 1 + combinations(3, 1)
              combinations(3, 1)
                return combinations(3 - 1, 1 - 1) + combinations(3 - 1, 1)
                  return 1 + combinations(2, 1)
                     combinations(2, 1);
                       return 1 + combinations(2 - 1, 1);
                         combinations(1, 1)
                           return 1
    So we have four 1 + somethings... that something eventually resolves into this = 1 + 1 + 1 + 1, which is 4, so you print 4.

    It just gets worse from there. Try a different approach to the whole problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pascal's Triangle
    By ijlalhayder in forum C Programming
    Replies: 1
    Last Post: 01-14-2011, 10:27 AM
  2. Pascal Triangle
    By illidari in forum C Programming
    Replies: 3
    Last Post: 05-03-2010, 09:44 PM
  3. pascal triangle
    By siavoshkc in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-25-2006, 01:21 PM
  4. pascal triangle
    By ndukaa1 in forum C++ Programming
    Replies: 15
    Last Post: 10-05-2004, 10:40 PM
  5. Pascal Triangle
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 01:34 PM