// ---------------------------------------------------------------------- // // Module: pascal.cc // Description: generates Pascal's triangle modulo 2 // with blanks representing 0's. // Contributed by: Rene Peralta // Date: 2001 // // ---------------------------------------------------------------------- #define UNKNOWN -1 #define MAX_SIZE 512 #include #include #include long num_rows; long PT[MAX_SIZE][MAX_SIZE]; void init(); long c(long n, long k); // returns the corr. binomial coefficient int main( int argc, char *argv[ ] ) { cout << "how many rows?" << endl; cin >> num_rows; init(); for (long n = 0; n < num_rows; n++) { for (long k = 0; k <= n; k++) if (c(n,k) == 1) cout << "1" ; else cout << " " ; cout << endl; } } long c(long n, long k) // returns the corr. binomial coefficient { if (PT[n][k] == UNKNOWN) PT[n][k] = (c(n-1,k-1) + c(n-1,k)) % 2; return(PT[n][k]); } void init() { for (long n = 0; n < num_rows; n++) for (long k = 0; k <= n; k++) if ((k==0) || (k==n)) PT[n][k] = 1; else PT[n][k] = UNKNOWN; }