#include <stdio.h>
Include dependency graph for Pascals.cpp:
Classes | |
struct | node |
struct | rowrec |
Typedefs | |
typedef node | NodePtr |
typedef rowrec | RowPtr |
Functions | |
float | fac (int n) |
float | C_n_r (int n, int r) |
void | generate_row (NodePtr **first, int rownum) |
void | make_table (int n, RowPtr **start) |
void | printrow (NodePtr *p) |
void | print (RowPtr *start) |
main () | |
Variables | |
RowPtr * | start |
int | col |
int | n |
float C_n_r | ( | int | n, | |
int | r | |||
) |
float fac | ( | int | n | ) |
void generate_row | ( | NodePtr ** | first, | |
int | rownum | |||
) |
Generate a row of Pascal's Triangle.
first | The data structure holding the triangle | |
rownum | Row to generate |
00060 {int i; 00061 NodePtr *p,*q; 00062 for (i=0;i<=rownum;i++) { 00063 if (*first != NULL) { 00064 q=new node; 00065 p->next=q; 00066 p=q; 00067 } 00068 else p=new node; 00069 if (*first==NULL) *first=p; 00070 p->coeff=C_n_r(rownum,i); 00071 p->next=NULL; 00072 } 00073 }
main | ( | ) |
Get rows and create triangle
00133 {start=new rowrec; 00134 printf(" How many rows (starting from 0) should be generated?"); 00135 printf(" Enter a Number between 0 and 12. Larger values cause"); 00136 printf(" Unreadable Results >"); 00137 scanf("%d",&n); 00138 printf("Pascal""s triangle for N=%d\n",n); 00139 make_table(n,&start); 00140 print(start); 00141 }
void make_table | ( | int | n, | |
RowPtr ** | start | |||
) |
Create Pascal's triangle
n | Rows to generate | |
start | The data structure holding the triangle |
00081 {int i; 00082 RowPtr *p; 00083 *start=new rowrec; 00084 (*start)->num=0; 00085 p=*start; 00086 p->thisrow=NULL; 00087 p->nextrow=NULL; 00088 generate_row(&(p->thisrow),0); 00089 for (i=1;i<=n;i++) { 00090 p->nextrow=new rowrec; 00091 p->nextrow->num=p->num+1; 00092 p=p->nextrow; 00093 p->thisrow=NULL; 00094 generate_row(&(p->thisrow),i); 00095 p->nextrow=NULL; 00096 } 00097 }
void print | ( | RowPtr * | start | ) |
Print the entire Triangle
start | rowptr heading first row |
00115 {float x; 00116 while (start!=NULL) { 00117 x=((n-(start->num-1))/2*6)-2; 00118 /* 00119 if (start->num % 2 == 0) 00120 gotoxy(x+2*(n-start->num+3),wherey()); 00121 else gotoxy(x+2*(n-start->num+1),wherey()); 00122 */ 00123 printrow(start->thisrow); 00124 printf("\n"); 00125 start=start->nextrow; 00126 } 00127 }
void printrow | ( | NodePtr * | p | ) |
int col |
int n |