Pascals.cpp File Reference

#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

RowPtrstart
int col
int n


Typedef Documentation

typedef struct node NodePtr

typedef struct rowrec RowPtr


Function Documentation

float C_n_r ( int  n,
int  r 
)

Compute the binomial coefficient.

Parameters:
n n in C(n,r)
r r in C(n,r)
Returns:
C(n,r)
00052 {return(fac(n)/(fac(n-r)*fac(r)));}

float fac ( int  n  ) 

Compute the factorial of an integer.

Parameters:
n the value for whose factorial will be computed
Returns:
the factorial of n
00040 {
00041  if (n<=1) return(1.0);
00042  else return(n*fac(n-1));
00043 }

void generate_row ( NodePtr **  first,
int  rownum 
)

Generate a row of Pascal's Triangle.

Parameters:
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

Parameters:
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

Parameters:
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  ) 

Print a single row of the triangle.

Parameters:
p Pointer to first element in row
00104   {while (p!=NULL) {
00105     printf("%7.0f   ",p->coeff);
00106     p=p->next;
00107    }
00108   }


Variable Documentation

int col

int n

RowPtr* start

Global variable is not necessary?


Generated on Sat Mar 21 20:21:56 2009 for DoxygenExample by  doxygen 1.4.7