Dec 2, 2011 at 9:00am Dec 2, 2011 at 9:00am UTC
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 Dec 2, 2011 at 12:42pm Dec 2, 2011 at 12:42pm UTC
Dec 2, 2011 at 9:12am Dec 2, 2011 at 9:12am UTC
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 Dec 2, 2011 at 9:12am Dec 2, 2011 at 9:12am UTC
Dec 2, 2011 at 9:22am Dec 2, 2011 at 9:22am UTC
You can get the largest number, count how many digits it has, then draw the triangle leaving that much space for every number.
Dec 2, 2011 at 9:40am Dec 2, 2011 at 9:40am UTC
(Of course then you would have to center number of n - 2 digits or less to make it look like a triangle.)
Dec 2, 2011 at 11:08am Dec 2, 2011 at 11:08am UTC
Add cout << "Tilt your head 45° to the left." << endl;
at the end.
Dec 2, 2011 at 11:15am Dec 2, 2011 at 11:15am UTC
Or better yet, find a way to change the monitor's rotation angle in your code.
Dec 2, 2011 at 12:41pm Dec 2, 2011 at 12:41pm UTC
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 Dec 2, 2011 at 12:50pm Dec 2, 2011 at 12:50pm UTC
Dec 5, 2011 at 8:41pm Dec 5, 2011 at 8:41pm UTC
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.
Dec 7, 2011 at 12:53pm Dec 7, 2011 at 12:53pm UTC
And what did you add in the lines 10 and 11? I can't get your point?
Dec 7, 2011 at 1:00pm Dec 7, 2011 at 1:00pm UTC
still trying to but can't get the same output as kev82
Dec 7, 2011 at 2:06pm Dec 7, 2011 at 2:06pm UTC
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 Dec 7, 2011 at 2:07pm Dec 7, 2011 at 2:07pm UTC
Dec 7, 2011 at 4:12pm Dec 7, 2011 at 4:12pm UTC
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?
Dec 7, 2011 at 7:16pm Dec 7, 2011 at 7:16pm UTC
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?
Dec 11, 2011 at 10:34am Dec 11, 2011 at 10:34am UTC
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:
Or implement something like this into your program... Let me know if you have questions about it.
Last edited on Dec 11, 2011 at 10:35am Dec 11, 2011 at 10:35am UTC
Jan 19, 2012 at 3:01pm Jan 19, 2012 at 3:01pm UTC
#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;
}