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

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
Friday, February 1, 2007 Minds are like parachutes. They only function when they are open. - Sir James Dewar, Scientist ( )
Introduction to C Programming CE
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Thursday, January 18, 2007 The question of whether computers can think is just like the question of whether submarines can swim. -Edsger W. Dijkstra (1930.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Friday, January 26, 2007 "One, demonstrations always crash. And two, the probability of them crashing goes up exponentially with the number of people watching."
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
1 Storage Classes, Scope, and Recursion Lecture 6.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Functions A function groups a set of related statements under a single title. You can "call" the function using its name. You may also provide parameters.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
C++ Programming Lecture 12 Functions – Part IV
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Program Development and Design Using C++, Third Edition
Function Recursion to understand recursion you must understand recursion.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
to understand recursion you must understand recursion
Functions.
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
5.13 Recursion Recursive functions Functions that call themselves
More on Recursive Recursion vs. Iteration Why Recursion?
C Functions Pepper.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1061 C Prgramming Lecture 11: Functions
Introduction to C++ Recursion
to understand recursion you must understand recursion
Recursion Chapter 11.
JavaScript: Functions Part II
Alternate Version of STARTING OUT WITH C++ 4th Edition
Functions Recursion CSCI 230
Simulating Reference Parameters in C
1-6 Midterm Review.
Functions Chapter No. 5.
Presentation transcript:

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

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.

§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

int squareByValue(int a); void squareByReference(int &aref); Calling Functions

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

int squareByValue(int a) { return a *= a; //caller's argument not modified } void squareByReference(int &cRef) { cRef *= cRef; // caller's argument modified } Calling Functions

§Function to swap two numbers

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

void f1(int *num) { cout<<*num<<endl; 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

131131

void f1(int* &num) { cout<<*num<<endl; 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?

133133

The most common use of references is in function parameters.

int a=6; int &b =a; cout<<b<<" "<<a<<endl; b=3; cout<<b<<" "<<a<<endl; References must be initialized Reference variables

6 3 Reference variables

What is wrong here? int a=6; int &b=a+4; cout<<b<<" "<<a<<endl; Reference variables

A function call ends when l Return statement is executed. l 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.

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

§Remember the even odd question?

Recursion A recursive function is one that calls itself. Recursive functions have two parts §Stopping Condition(s) -- solving the base case(s). §Recursive call -- breaking the problem down.

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.

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); //... }

§Focus on the flow of control

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

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

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

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

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

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

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

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

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

ans=55

Recursion vs. Iteration Similarities: 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.

Recursion vs. Iteration Similarities: 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.

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 Search int 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 Search int 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); }