Hello to alll programmers out there.
Here i am trying to design a custom pascal triangle where the user can specify the TOP ELEMENT as well as both the CORNER elements.
The problem i am facing is that the prog works well till the 3rd row but starts giving rubbish values after 3rd. Looks like there is some problem with my logic. I want to do this problem using only one array and not two arrays.
Any suggestions?
#include "stdafx.h"
#include <stdio.h>
const int leftCorner = 1;
const int rightCorner = 1;
const int topElement = 1;
int *pascalTriangle = 0;
int buildTriangle (int tmpPascalRow)
{
int prev, curr, next;
int tmpRow, elementIndex = 0;
int buffer = 0;
if (tmpPascalRow == 1)
{
pascalTriangle[0] = topElement;
return 1;
}
pascalTriangle[0] = leftCorner;
pascalTriangle[1] = rightCorner;
for (tmpRow = 3; tmpRow <= tmpPascalRow; ++tmpRow)
{
pascalTriangle[0] = leftCorner;
pascalTriangle[tmpRow - 1] = rightCorner;
buffer = leftCorner;
for (elementIndex = 1; elementIndex < tmpRow - 1; ++elementIndex)
{
buffer = pascalTriangle[elementIndex];
pascalTriangle[elementIndex] += buffer;
}
}
}
int main (void)
{
char inputBuffer[10];
int pascalRow = 0;
int index;
fputs ("Enter the number of rows required: ", stdout);
fgets (inputBuffer, sizeof (inputBuffer), stdin);
pascalRow = atoi (inputBuffer);
pascalTriangle = (int*) calloc (pascalRow, sizeof (int));
buildTriangle (pascalRow);
for (index = 0; index < pascalRow; ++index)
printf ("%d\t", pascalTriangle[index]);
getchar();
return 0;
}