Friday, January 26, 2007
"One, demonstrations always crash.
And two, the probability of them crashing goes up exponentially
with the number of people watching."
- Steve Jobs
SELF TEST: Recursion
int factorial(int N){
int ans;
if (N<=1) return 1; //base case
ans=factorial(N-1)*N;
return ans;
}
int main() {
int answer=factorial(4);
cout<<answer;
return 0; }
Stack contents
Example: Binary Searchint BinarySearch(int no, int low,int high, int* iarray);int main(void) {
int num, i, size=15;
int* array=new int[size];
for (i=0; i<size; i++)
array[i]=13*i+4;
for (i=0; i<size; i++)
cout<<array[i]<<" ";
cout<<endl;
cin>>num;
if(BinarySearch(num, 0, size-1, array))
cout<<"found"<<endl;
else
cout<<"not found"<<endl;
return 0; }
Example: Binary Searchint BinarySearch(int no, int low,int high, int* iarray){
if (low>high){
return 0; }
int mid=low+(high-low)/2;
if (no==iarray[mid])
return 1;
else if (no>iarray[mid])
return BinarySearch(no, mid+1, high, iarray);
else
return BinarySearch(no, low, mid-1, iarray);
}
void recFunc(int i){if ((i/2)==0){
cout<<i%2;return;
}recFunc(i/2);cout<<i%2;
}int main(void){
int i;cin>>i;if (i<0){
cout<<"Sorry -ve nos not allowed"<<endl;
exit(1); }recFunc(i);return 0; }
Example Using Recursion: The Fibonacci Series
The Fibonacci series: 1,1,2,3,5,8,13,21,… begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two.
The function to calculate the i-th Fibonacci looks like what?
What would the code for this would look like ?
Example Using Recursion: The Fibonacci Series
int fib(int n){int ans;
if (n<1)
ans= -1; //error condition
else if (1==n)
ans=1;
else if (2==n)
ans=1;
else
ans=fib(n-1)+fib(n-2);
return ans;
}
Recursionbool myFunction(int n, int k);
int main(void){
cout<<myFunction(5678, 4)<<endl;
cout<<myFunction(2359, 5)<<endl;
return 0;
}
bool myFunction(int n, int k){
if (n/10 == 0) return n==k;
else if (n%10 == k ) return true;
else return myFunction(n/10, k);
}
void RecLast(int *a, int i){
if(4==i) return;
*a *=2;
cout<<*a<<" ";
++i;
RecLast(a+1,i); }
int main() {
int i, index=0;
int array[]={1,2,3,4};
RecLast(array, index);
for (i=0; i<4; i++){
cout<<array[i]; }
return 0; }
SELF TEST
void RecFirst(int *a, int i){
if(i==4) return;
++i;
RecFirst(a+1,i);
*a *=2;
cout<<*a<<" "; }
int main() {
int i, index=0;
int array[]={1,2,3,4};
RecFirst(array, index);
for (i=0; i<4; i++){
cout<<array[i]; }
return 0; }
SELF TEST
SELF TEST: What is wrong here?
int fact(int N){
int ans;
ans=fact(N-1)*N;
return ans;
}
int main() {
cout<<fact(4);
return 0;
}
Recursionvoid output(char *s);
int main() {
char str[] = {“stay"};
output(str);
cout<<endl;
return 0; }
void output(char *s) {
if(*s)
output(s+1);
else
return;
cout << *s;
}
Recursion vs. IterationRecursion has many negativesIt repeatedly invokes the mechanism, and
consequently the overhead, of function calls.This can be expensive in both terms of processor
time and memory.
So Why use recursion?Some problems map very nicely to a recursive
solution. Thereby making code writing easier.
void H(int topLeftX,int topLeftY,int bottomLeftY,int depth)
{int bottomLeftX=topLeftX;int topRightX=topLeftX+(bottomLeftY-topLeftY);int topRightY=topLeftY;int bottomRightX=topRightX;int bottomRightY=bottomLeftY;
if (depth==0) return; //stopping condition
//Code for drawing lines
int oneFourth=(bottomLeftY-topLeftY)/4;
H(topLeftX-oneFourth, topLeftY-oneFourth, topLeftY+oneFourth, depth-1);
H(topRightX-oneFourth, topRightY-oneFourth, topRightY+oneFourth, depth-1);
H(bottomLeftX-oneFourth, bottomLeftY-oneFourth, bottomLeftY+oneFourth, depth-1);
H(bottomRightX-oneFourth, bottomRightY-oneFourth, bottomRightY+oneFourth, depth-1);
}
Defining a new name for existing type
typedef float balance;
balance over_due;
balance is another name for float.
typedef
char wordsb[11][80]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" };
int i=1;
while(strcmp(wordsb[i],"")) {
cout<<wordsb[i]<<" ";
i+=3;
}
char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" };
int i=1;
while(strcmp(words2[i],""))
{
cout<<words2[i]<<" ";
i+=3;
}
Memory Drawing!
char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" };
int i=1;
while(words2[i][0])
{
cout<<words2[i]<<" ";
i+=3;
}