Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Pascal's Triangle (Unstable Code)

Tags:

c

codeblocks

There are two questions concerning the code posted below:

    1) When I run this code on CodeBlocks, the code sometimes successfully runs (returning 0) but usually results in an error after it shows all the results (returning -1073741819). Why is this the case?

    2) The values are all correct except for the last element of the array where the value should be 1 (pTriangle[20] = 1). However, I am getting some garbage number at the end, what am I doing wrong?

I have realized I could arrive at the same result with binomial coefficients but I still have no idea why I am getting the error and it'd be best if my mistake can be found.

Update1:
pTriangle[i] = temp[i % 2 ? 0 : 1] + pTriangle[i]; seems to be the problem. When I have commented this code, the program did not crash. I am trying to find out why it is crashing and trying to find a solution around it :)

#include <stdio.h>
#include <stdlib.h>

#define LEVEL 20

int main()
{
    int *pTriangle = (int*)malloc(sizeof(int)*(LEVEL+1));
    int i;

    for (i = 0; i < LEVEL; i++)
        pTriangle[i] = 0;

    createPascalTriangle(pTriangle, LEVEL);

    for(i = 0; i < LEVEL+1; i++)
        printf("pTriangle[%d]: %d\n", i, pTriangle[i]);

    free(pTriangle);

    return 0;
}

int createPascalTriangle(int *pTriangle, int level){
    if (level <= 0)
        return 0;
    pTriangle[0] = 1;
    pTriangle[1] = 1;
    int i;
    for ( i = 2; i <= level; i++)
        increasePascalTriangleOneLevel(pTriangle);

    return 1;
}

int increasePascalTriangleOneLevel(int *pTriangle){
    int i = 1;
    int temp[2] = {0};

    temp[0] = pTriangle[0];
    while (pTriangle[i] != 0){
        temp[i % 2] = pTriangle[i];
        pTriangle[i] = temp[i % 2 ? 0 : 1] + pTriangle[i];
        i++;
    }
    pTriangle[i] = 1;

    return 1;
}
like image 543
kpark Avatar asked Jul 04 '13 20:07

kpark


People also ask

How do you code Pascal's Triangle in C++?

Pascal's Triangle in C++ The top row is numbered as n=0, and in each row are numbered from the left beginning with k = 0. Each number is found by adding two numbers which are residing in the previous row and exactly top of the current cell. It is also being formed by finding (𝑛𝑘) for row number n and column number k.

How do you create Pascal's triangle?

Pascal's Triangle Construction The easiest way to construct the triangle is to start at row zero and write only the number one. From there, to obtain the numbers in the following rows, add the number directly above and to the left of the number with the number above and to the right of it.

How do you find the nth term in a Pascal triangle?

The number of elements in the nth row is equal to (n + 1) elements. Pascal's triangle can be constructed by writing 1 as the first and the last element of a row and the other elements of the row are obtained from the sum of the two consecutive elements of the previous row.


1 Answers

The last element of the array hasn't been initialized.

Write:

for (i = 0; i < LEVEL + 1; i++)
    pTriangle[i] = 0;

instead of:

for (i = 0; i < LEVEL; i++)
        pTriangle[i] = 0;
like image 115
szedjani Avatar answered Oct 20 '22 00:10

szedjani