// PascalsTriangle.c // Calculate and display rows of Pascal's Triangle. #include typedef unsigned int WORD; /* Maximum number of rows in triangle. */ #define MAX 50 /* Number of rows to display. */ #define NROW 20 /* Field width for display of each number. */ #define WIDTH 6 /* Total number of columns to display in. */ #define COL 135 /* Array to hold rows of triangle - seed with first row. */ WORD t[MAX] = {1}; void NextRow(WORD a[]); void main(void) { int i, j; /* to index for loops */ WORD n = 0; for (i = 1; i < NROW; i++) { printf("%*c", (COL-WIDTH*(i+1))/2, ' '); for (j = 0; j < NROW; j++) { if (t[j]) printf("%*u", WIDTH, t[j]); else break; } printf("\n\n"); NextRow(t); } // for i } // main() void NextRow(WORD a[]) /* USE: Calculate next row of Pascal's triangle. IN: a[] = last row, padded right with 0's OUT: a[] = next row, padded right with 0's */ { int i; /* to index for loop */ WORD b[MAX]; /* store old row before calc next one */ /* Copy incoming row into b[]. */ for (i = 0; i < MAX; i++) b[i] = a[i]; /* Apply addition formula, including 0 on right.*/ for (a[1] = 1, i = 1; i < MAX; i++) a[i] = b[i] + b[i-1]; }