-
Pascal Triangle
Ok, i have written this program w/ a one-dim. array, but the assignment calls for a two-dim. array. With the two-dim array on the output i get a weird negative number as follows :
Output :
1
-895433063 1
The one's are in the correct places, but the -number is screwing me up.
Here's my code :
//Global Varibles
const long int MAX_ROWS = 10;
const long int MAX_COLS = 10;
//Begin Main
void main(void)
{
//Variables
long last[MAX_ROWS][MAX_COLS];
long current[MAX_ROWS][MAX_COLS];
int x;
int y;
int size;
int find = 0;
current[1][0] = 1;
last[0][0] = 0;
last[1][0] = 1;
int a = 1;
int b = 0;
//Statements
cout << "Enter in size of triangle : ";
cin >> size;
if(size == 1)
{
printf("1\n");
}
else
{
printf("1\n");
while (1)
{
a++;
b++;
find = 0;
for (x = 1; x < a; x++)
{
for(y = 0; y < b; y++)
{
current[x][y] = last[x - 1][y] + last[x - 1][y - ];
}
}
for (x = 0; x < a; x++)
{
for(y = 0; y < b; y++)
{
last[x][y] = current[x][y];
last[a][b] = 1;
}
}
for (x = 1; x <= a; x++)
{
for(y = 1; y <= b; y++)
{
if(size < 4)
{
size = size - 1;
}
if (last[x][y] > size)
{
find = 1;
}
if (x > 1)
printf(" %ld", last[x][y]);
else
printf("%ld", last[x][y]);
}
}
printf("\n");
if(find)
{
break;
}
}// end While
}//End else
getch();
}
//End Main, End Program
-JMM
-
i didnt read it but large weird negative numbers mean you probably didnt ever chnage the data in the variable from its inital value.
-
I wish you could include more descriptions with your code. It would be easier to follow.
you only need one 2-D array and 4 loops.
triangle[0][0]=1;
for(int a=1; a<size; a++)
triangle[a][0]=1;
for(int b=1; b<a; b++)//you need to finish this loop
//find all values for triangle[MAX_ROWS][MAX_COLS]
for(int a=1; a<size; a++)
cout << triangle[a-1][0];
for(int b=1; b<a; b++)
cout << triangle[a-1][b];
cout << endl;
//print the triangle from one matrix