-
October 20th, 2009, 07:59 AM
#1
Pascal's Triangle Using For Loops
Ok, so this is what i have...
When i build it I just get a vertical line of 1's. There are no variables the task is straight forward. We have to make a program the when executed displays the first 10 rows. I'm not good at this and im also not looking for someone to write the program for me I just need advice as I can't find the problem. I think I can do the formatting the output by myself but I'd like to get it to output the right stuff first.
Thanks in advance
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void main ()
{
// Define variables
int n; // "n things"
int k; // "k times"
int K; // Used to help calculate factorial
int N; // Used to help calculate factorial
int a; // "n - k" renamed
int A; // Used to help calculate factorial
int number; // Final output number
// For Loops to calculate factorials
for (n = 0; n <= 10; n+= 1)
{
for (k = 0; k <= n; k += 1)
{
if (n = 0 || 1)
N = 1;
else
{
N = n;
for (n = n; n > 1; n--)
N = N * (n-1);
}
if (k = 0 || 1)
K = 1;
else
{
K = k;
for (k = k; k > 1; k = k - 1)
K = K * (k-1);
}
a = n - k;
if (a = 0 || 1)
A = 1;
else
{
A = a;
for (a = a; a > 1; a = a - 1)
A = A * (a-1);
}
number = N / (K * A);
cout << number << endl;}
// Output
}}
-
October 20th, 2009, 03:06 PM
#2
Re: Pascal's Triangle Using For Loops
Take a look at this statement...several more like it in your code...this should have kicked up a warning when you compiled...you are assigning (NOT comparing) the value of "0" ZERO to "n" within an if statement.
Last edited by Vanaj; October 20th, 2009 at 03:27 PM.
Jim
ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII
"The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.
"Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.
-
October 21st, 2009, 10:58 AM
#3
Re: Pascal's Triangle Using For Loops
Hey i learnt and tried to implement pascal triangle. It may not be perfect but it works great.
#include <iostream>
using namespace std;
int main()
{
int rows, rows2, space;
cout<<"\n Enter the no.of rows to be displyed in the pascal triangle\t";
cin>>rows;
rows2= rows;
//int a[][] = new int [rows][rows];
int a[15][15];
int i=0,j=0;
if (rows ==1)
{
a[i][j]=1;
cout<<"\n" <<a[i][j];
}
else if (rows == 2)
{
a[i][j]=1;
cout<<"\n"<<"\t" << a[i][j];
i++;
a[i][j]=a[i][j+1]=1;
cout<<"\n" <<a[i][j] <<"\t\t" << a[i][j+1] ;
}
else
{
a[i][j]=1;
cout<<"\n";
for (space =1; space <= rows2; space ++)
cout<<"\t";
cout<< a[i][j];
i++; rows2--;
a[i][j]=a[i][j+1]=1;
cout<<"\n";
for (space =1; space <= rows2; space ++)
cout<<"\t";
cout<<a[i][j] <<"\t\t" << a[i][j+1] ;
rows2--;
//cout<<rows2;
for (i=2;i<rows;i++)
{
cout<<"\n";
for (space =1; space <= rows2; space ++)
cout<<"\t";
rows2--;
for (j=0;j<=i;j++)
{
if ( j==0)
{
a[i][j]=1;
}
else if ( i==j)
{
a[i][j]= 1;
}
else
a[i][j] = a[i-1][j-1] + a[i-1][j];
cout<< a[i][j]<<"\t\t" ;
}
}
}
int stop;
cout<<"\n";
cin>>stop;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|