Increased Pascal Triangle printed with wrong values - C

53 Views Asked by At

I want to print a triangle that is similar to Pascal Triangle but the sides are increased instead of containing the value 1.

Regular Pascal Triangle:

Regular Pascal Triangle

Wanted Triangle:

Wanted Triangle

Regular Pascal method:

void PascalTriangle(int rows) {
    int i =0,j = 0,space,coef = 0;
    for (i = 0 ; i<rows ; i++){
        for (space = 1 ; space <= rows - i ; space++) 
            printf(" ");
        
        for (j = 0 ; j <= i ; j++) {
            if (i == 0 || j == 0)
                coef = 1;
            else  
                coef = coef * (i - j + 1) / j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

what I was trying to do:

void PascalTriangle(int rows) {
    int i =0,j = 0,space,coef = 0;
    for (i = 0 ; i<rows ; i++){
        for (space = 1 ; space <= rows - i ; space++) 
            printf(" ");
        
        for (j = 0 ; j <= i ; j++) {
            if (i == 0 || j == 0)
                coef ++;
            else  
                coef = coef * (i - j + 1) / j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

When increasing coef my output looks good only on the sides of the triangle:

My output

I would like to clarify - I am not looking for a solution but to learn where I went wrong, I will appreciate any help.

1

There are 1 best solutions below

0
picchiolu On

As you ask for directions and not for a solution, I'll say that you cannot use the same index algebra as in the standard Pascal's triangle formula when the outer coefficients aren't equal to one.