2c03-review - 00066

2c03-review - 00066 - //initializing the table for(int...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
9 int Factorial(int n){ int f=1; for(int i=1;i<=n;i++) f*=i; return f; } //DnC implements solution to 10(a). it uses divide and conquer method int DnC(int n,int m){ if(m==0 || n==m) return 1; else return DnC(n-1,m)+DnC(n-1,m-1); } //DP implements solution to 10(c). it uses dynamic programing technique method int DP(int n,int m){ //allocating the table int **L; L=new int*[n+1]; for(int i=0;i!=n+1;i++) L[i]=new int[m+1];
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //initializing the table for(int i=0;i!=n+1;i++) L[i][0]=1; for(int i=0;i!=m+1;i++) L[i][i]=1; //filling the table for(int i=1;i!=n+1;i++) for(int j=1;j!=m+1&&j<i;j++) L[i][j]=L[i-1][j]+L[i-1][j-1]; //result is in L[n][m] int r=L[n][m]; for(int i=0;i!=n+1;i++) delete L[i]; delete L; return r; } void main(){ cout<<"DnC(14,10):\t"<<DnC(14,10)<<endl; cout<<"DP(14,10):\t"<<DP(14,10)<<endl;...
View Full Document

This note was uploaded on 12/10/2009 for the course CAS 2c03 taught by Professor Janicki during the Spring '03 term at McMaster University.

Ask a homework question - tutors are online