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:
1 2 3 4 5 6
|
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:
1 2 3 4 5 6
|
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:
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
|
#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;
}
}
|
1st cout:
2nd cout:
3rd cout:
If you need further help, let me know.
Topic archived. No new replies allowed.