Code:
#include <stdio.h>
int main() {
int pascaltri[11][11];
int r, c;
// Initialize beginning and end of each
// row to 1.
for (r=0; r<11; r++) {
pascaltri[r][0] = 1;
pascaltri[r][r] = 1;
}
// Fill in the table.
for (r=2; r<11; r++)
for (c=1; c<r; c++)
pascaltri[r][c] = pascaltri[r-1][c-1]+
pascaltri[r-1][c];
// Loop through each row of the table.
for (r=0; r<11; r++) {
// Loop through each value of the row.
for (c=0; c<=r; c++)
printf("%4d ", pascaltri[r][c]);
// Go to the next line.
printf("\n");
}
return 0;
}
I am trying to figure out how this works, it is the code to pascal's triangle. I understand I am making a 11x11 triangle and the reason a 1 appears upfront and at the end of each line. I don't understand the part that fills in the lines.
Code:
for (r=2; r<11; r++)
for (c=1; c<r; c++)
pascaltri[r][c] = pascaltri[r-1][c-1]+
pascaltri[r-1][c];
For the first line this fills in , is it doing this?
r=2 c=1
[2-1][1-1]+[2-1][1]
[1][0]+[1][1]
Im not sure what it is doing with that information from there. Can someone explain Also how does this cause multiple entries? Like in row 3, there is two numbers it is entering in.