Pascal triangle

Hi to everyone!!!

I wonder if anybody can give me a hand with a problem.

I have to write a program which displays a Pascal Triangle (Which starts with one an as long as it goes adds value of previous raw).

I could come up with a code which does it. BUT!! I DON'T KNOW HOW TO MAKE A PERFECT TRIANGLE. What I have is like this:

1
2
3
4
5
6
7
1                                                                                                             
1 1                                                                                                           
1 2 1                                                                                                         
1 3 3 1                                                                                                       
1 4 6 4 1                                                                                                     
1 5 10 10 5 1 


but it should be like this:

1
2
3
4
5
6
     1                                                                                                             
    1 1                                                                                                           
   1 2 1                                                                                                         
  1 3 3 1                                                                                                       
 1 4 6 4 1                                                                                                     
1 5 10 10 5 1 


I don't know what to do. Can anybody give me a hint. This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
int n,k,i,x;
cout << "Enter a row number for Pascal's Triangle: ";
cin >> n; //the number of raws
for(i=0;i<=n;i++)
{
x=1;
for(k=0;k<=i;k++)
{
cout << x << '\t';
x = x * (i - k) / (k + 1);
}
cout << endl;
}
return 0;
}


Last edited on
Making a perfect triangle would be hard, because the number of digits increases as you work your way down. However, not that what you have above, the bottom row (row n) is padded with 0 spaces on the left. and the top row is padded with n spaces. Can you see the patteren of padding vs row number? Since the current row is i in your program you should be good from there.
Last edited on
You can get the largest number, count how many digits it has, then draw the triangle leaving that much space for every number.
(Of course then you would have to center number of n - 2 digits or less to make it look like a triangle.)
Add cout << "Tilt your head 45° to the left." << endl; at the end.
Or better yet, find a way to change the monitor's rotation angle in your code.
Thanks Lads!!!
Nice joke about cout your had. But for me it's nor funny :(

I tried to follow your advice. I declared a variable y and make it equal to n int y = n; And laiter in the beginning of the first loop I added this:
1
2
3
4
y = n;
for(int s =0; s <= y/2; s++){
  cout<< '\t';
}


but the problem is still there. The last line doesn't match the construction of the triangle. Like this:
1
2
3
4
5
6
7
                                1
                        1       1
                        1       2       1
                1       3       3       1
                1       4       6       4       1
        1       5       10      10      5       1
        1       6       15      20      15      6       1


And what if the input is odd? I can't cout 1/2 of a space. Can i? :)
Last edited on
Yea, tabs are nice for tables, but not so much for triangles... Just pad the left with spaces. Honestly, it doesn't make much senses to format the information going to standard output. You should print them the way you had them originally, and then pipe the output to another pogram that just centers the text for you.
Tabs are perfectly good for triangles. Add a new line between lines 10 and 11, and alter line 13 and you'll get this:

1
2
3
4
5
6
7
8
Enter a row number for Pascal's Triangle: 5
					1		
				1		1		
			1		2		1		
		1		3		3		1		
	1		4		6		4		1	
1		5		10		10		5		1
Kevins-MacBook-Pro:~ kev82$  


Admittedly, it's not what the assignment asks for, but I think it looks much better.
And what did you add in the lines 10 and 11? I can't get your point?
still trying to but can't get the same output as kev82
Ok, here is the same output, but every tab has been replaced with the letter t. That should help you figure it out.

1
2
3
4
5
6
7
tttttt1tt
ttttt1tt1tt
tttt1tt2tt1tt
ttt1tt3tt3tt1tt
tt1tt4tt6tt4tt1tt
t1tt5tt10tt10tt5tt1tt
1tt6tt15tt20tt15tt6tt1tt


Note how many 't's there are between numbers and how many 't's there are at the start of each row.
Last edited on
ok get you. I should change cout << x << '\t'; to cout << x << '\t' << '\t';
but what do i have to add in between 10 and 11 lines?
How many tabs are there on the first line? How many tabs are there on the second line? Is there some kind of pattern between the number of tabs I print at the start of the line and the line number?
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
27
28
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
	unsigned short cols = 80;
	if( argc > 1 ) {
		stringstream ss;
		ss << argv[1];
		if( !(ss >> cols) ) {
			cerr << "Bad argument!\n";
			return 1;
		}
	}
	string line;
	while( getline(cin, line) ) {
		if( line.length() < (cols - 1) ) {
			unsigned short pad = (cols - line.length()) / 2;
			do
				line.insert( line.begin(), ' ' );
			while( --pad );
		}
		cout << line << '\n';
	}
	return 0;
}
center.cpp
Run your program like this:
%> pascal_tri | center

Or implement something like this into your program... Let me know if you have questions about it.
Last edited on
#include<stdio.h>
#include<iostream>
using namespace std;
long double fact(int n)
{
long double prod=1.0;
if(n==0 || n==1)return 1.0;
else for(int i=n;i>1;i--)
prod *=i;return prod;
}
int func(int n)
{
long double* a = new long double[n/2+1];
for(int i=0;i<=n/2;i++)
{
a[i]=fact(n)/(fact(i)*fact(n-i));
}
for(int i=0;i<=n/2;i++)
{
cout<<a[i]<<"\t";
}
for(int i=n/2-1;i>=0;i--)
{
cout<<a[i]<<"\t";
}
cout<<"\n";
}
int main()
{
int n;scanf("%d",&n);
func(n);
system("pause");
return 0;
}
Topic archived. No new replies allowed.