Kodewick
Start learning →
Basic←→0 / 50

Pascal's triangle

#032Basict[i - 1][j - 1] + t[i - 1][j]8 min

In Pascal's triangle, each row starts and ends with 1, and each number between is the sum of the two above it (up-left and straight up). The first 4 rows are 1, 1 1, 1 2 1, 1 3 3 1. Finish pascal(n) to show the first n rows, left-aligned, one space apart. It shows 5 and 6 rows. t[i][j] is row i, item j (both from 0).

Should show
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
main.c
#include <stdio.h>

void pascal(int n) {
    int t[20][20] = {0};
    for (int i = 0; i < n; i++) {
        t[i][0] = 1;
        t[i][i] = 1;
        for (int j = 1; j < i; j++) {
            // your code here: work out t[i][j] from two numbers in the row above
        }
        for (int j = 0; j <= i; j++) printf(j ? " %d" : "%d", t[i][j]);
        printf("\n");
    }
}

int main(void) {
    pascal(5);
    pascal(6);
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.