thursday, january 25, 2007

40
Thursday, January 25, 2007 "To define recursion, we must first define recursion." - Anonymous

Upload: aurelia-bass

Post on 30-Dec-2015

46 views

Category:

Documents


2 download

DESCRIPTION

Thursday, January 25, 2007. " To define recursion, we must first define recursion. " - Anonymous. Remaining Lectures Schedule. Thursday January 25 (Today) Friday January 26 Thursday February 1 Friday February 2 Saturday February 3 Tuesday February 6. What is wrong here? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Thursday, January 25, 2007

Thursday, January 25, 2007

"To define recursion, we must first define recursion."

- Anonymous

Page 2: Thursday, January 25, 2007

Remaining Lectures Schedule

Thursday January 25 (Today)Friday January 26Thursday February 1Friday February 2Saturday February 3Tuesday February 6

Page 3: Thursday, January 25, 2007

What is wrong here?

int* function_returns_ptr(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

return (ptrArray+4);

}

int main(void){

int *result=function_returns_ptr();

cout<<*result<<endl;

return 0;

}

Page 4: Thursday, January 25, 2007

References

Safer and simpler use of memory addresses are references

When a reference parameter is used, the address of an argument is automatically passed to the function.

Within the function the operations on reference parameter are automatically de-referenced.

Page 5: Thursday, January 25, 2007

With call by reference, the caller gives the called function the ability to directly access the caller’s data and to modify that data if the called function so chooses.

To indicate that a function parameter is passed by reference, simply follow the parameter’s type in the function prototype by an ampersand &.

e.g. void myFunction( int &x);

Calling Functions

Page 6: Thursday, January 25, 2007

int squareByValue(int a);

void squareByReference(int &aref);

Calling Functions

Page 7: Thursday, January 25, 2007

int main() {

int x = 2, z = 4;

cout << "x = " << x << " before squareByValue" ;

squareByValue(x) ;

cout<< "x = " << x << " after squareByValue" ;

cout << "z = " << z << "before squareByReference”;

squareByReference(z);

cout << "z = " << z << " after squareByReference";

return 0;

}

Calling Functions

Page 8: Thursday, January 25, 2007

int squareByValue(int a)

{

return a *= a; //caller's argument not modified

}

void squareByReference(int &cRef)

{

cRef *= cRef; // caller's argument modified

}

Calling Functions

Page 9: Thursday, January 25, 2007

Function to swap two numbers

Page 10: Thursday, January 25, 2007

void swap(int &sa, int &sb){

int temp=sa;

sa=sb;

sb=temp; }

int main() {

int a=3;

int b=5;

cout<<a<<" "<<b<<endl;

swap(a, b);

cout<<a<<" "<<b<<endl;

return 0; }

Calling Functions -II

Page 11: Thursday, January 25, 2007

void f1(int *num) {

cout<<*num<<endl;

num++;

num++;

cout<<*num<<endl;}

int main(void){

int a[8]={1,2,3,4,5,6,7,8};

int *aptr=a;

f1(aptr);

cout<<*aptr<<endl;return 0;} //OUTPUT?

Calling Functions with pointers

Page 12: Thursday, January 25, 2007

1

3

1

Calling Functions with pointers

Page 13: Thursday, January 25, 2007

void f1(int* &num) {

cout<<*num<<endl;

num++;

num++;

cout<<*num<<endl;}

int main(void){

int a[8]={1,2,3,4,5,6,7,8};

int *aptr=a;

f1(aptr);

cout<<*aptr<<endl;return 0;} //OUTPUT?

Page 14: Thursday, January 25, 2007

1

3

3

Page 15: Thursday, January 25, 2007

The most common use of references is in function parameters.

Page 16: Thursday, January 25, 2007

int a=6;

int &b =a;

cout<<b<<" "<<a<<endl;

b=3;

cout<<b<<" "<<a<<endl;

References must be initialized

Reference variables

Page 17: Thursday, January 25, 2007

6 6

3 3

Reference variables

Page 18: Thursday, January 25, 2007

What is wrong here?

int a=6;

int &b=a+4;

cout<<b<<" "<<a<<endl;

Reference variables

Page 19: Thursday, January 25, 2007

A function call ends when Return statement is executed. The closing } brace of the end of

function is reached.

At the end of function call, flow of control returns back to the calling function.

Page 20: Thursday, January 25, 2007

void X() {

int a = 1;

int b = 2;

// T1

Y(a);

// T3

Y(b);

// T5

}

void Y(int p) {

int q;

q = p + 2;

// T2 (first time through), T4 (second time through)

}

Page 21: Thursday, January 25, 2007

Remember the even odd question?

Page 22: Thursday, January 25, 2007

RecursionA recursive function is one that calls itself.

Recursive functions have two partsStopping Condition(s) -- solving the base

case(s).Recursive call -- breaking the problem

down.

Page 23: Thursday, January 25, 2007

Recursion

When a function calls itself, new local variables and parameters are allocated storage on stack and the function is executed with these new variables from the beginning of function.

Page 24: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;if (x<=0)

return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;

}

int main(void){PrintFunc(3);

//...}

Page 25: Thursday, January 25, 2007

Focus on the flow of control

Page 26: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

Page 27: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

x is 2

Page 28: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

x is 2

x is 1

Page 29: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

x is 2

x is 1

x is 0

Page 30: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

x is 2

x is 1

Page 31: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

x is 2

Page 32: Thursday, January 25, 2007

void PrintFunc(int x){ int y=x-1;

if (x<=0) return;cout<<"In Print Func before recursive call. x="<<x<<endl;PrintFunc(y);cout<<"In Print Func after recursive call. x="<<x<<endl;}

x is 3

Page 33: Thursday, January 25, 2007

In Print Func before recursive call. x=3In Print Func before recursive call. x=2In Print Func before recursive call. x=1In Print Func after recursive call. x=1In Print Func after recursive call. x=2In Print Func after recursive call. x=3

Page 34: Thursday, January 25, 2007

int RecFunc(int x){int sum;if(x<=1)

return 1;sum = x*x + RecFunc(x-1);cout<<sum<<endl;return sum;

}

int main(void){int ans;ans=RecFunc(5);cout<<"ans="<<ans<<endl;//...

}

Page 35: Thursday, January 25, 2007

5

14

30

55

ans=55

Page 36: Thursday, January 25, 2007

Recursion vs. IterationSimilarities:

Both iteration and recursion involve repetition:Iteration explicitly uses a repetition structure;recursion achieves repetition through repeated

function calls.

Iteration and recursion each involve a

termination test iteration terminates when the loop condition fails recursion terminates when a base case is

recognized.

Page 37: Thursday, January 25, 2007

Recursion vs. IterationSimilarities:

Both iteration and recursion can occur

infinitely:An infinite loop can occur with iteration,Infinite recursion can occur when the base case is

not reached.

Page 38: Thursday, January 25, 2007

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

Page 39: Thursday, January 25, 2007

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; }

Page 40: Thursday, January 25, 2007

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);

}