Pascal's triangle with modification

115 Views Asked by At

I wrote a function to print Pascal's triangle with modifications. The sides of the triangle will appear in ascending order instead of the value 1. Each values at the base of the triangle will continue to contain the sum of the 2 members above it. Also need to print the max value of a given level. Example :

Pascal Triangle With Modification Example

1

void printTriangle2(int rows,int level) {
    int i =0,j = 0,space,res = 0, isLevel = 0,max = 0;
    for (i = 0 ; i<rows ; i++){
        if (i == level)
            isLevel = 1;
        else isLevel = 0;
        for (space = 1 ; space <= rows - i ; space++) {
            printf(" ");
        }
        for (j = 0 ; j <= i ; j++) {
            if (i == 0 || j == 0)
                res++;
            else  {
                res = res * (i - j + 1) / j;
            }
            if (isLevel == 1) {
               if (res > max)
                max = res;
            }
            printf("%4d",res);
        }
        printf("\n");
    }
    printf(" the max value is : %d",max);
}

my output for the input rows = 5 , level = 3:

Output Example

2

I have an issue with the calculation of res if the value is not in the side of the triangle

res = res * (i - j + 1) / j;

What am I doing wrong?

0

There are 0 best solutions below