PIC10A Lecture 26

Recursion

Reading

p549-567(skip 558, 559)

Recursive void fucntion


Example
#include<iostream>
using namespace std;

void print(int x);

int main() {
print(3);
return 0;
}

void print(int x) {
if(x <= 0) { // base case cout << "Stop at x = 0\n"; } else { cout << "Calling print(" << x << ")\n"; print(x-1); // call the function again } }

Output
Calling print(3)
Calling print(2)
Calling print(1)
Stop at x = 0
Explanation









How recursion works How recursion ends
What is the output of the following function
#include<iostream>
using namespace std;

void print(int x);

int main() {
print(3);
return 0;
}

void print(int x) {
cout << "Calling print(" << x << ")\n";
print(x-1); // call the function again
}
What is wrong about the function?








Another example
#include<iostream>
using namespace std;

// print the digits of the number n vertically
void writeVertical(int n);

int main() {
writeVertical(2593);
return 0;
}

void writeVertical(int n) {
if(n < 10) { // base case cout << n << endl; } else { // n is two or more digits long writeVertical(n/10);
cout << (n%10) << endl;
} }

Output
2
5
9
3

Recursive functions that return a value


Example
#include<iostream>
#include<cstdlib> // for exit
using namespace std;

// return x to the power n
int power(int x, int n);
int main() { cout << power(5,3) << endl; return 0; } int power(int x, int n) { if(n < 0) { cout << "Illegal argument to power.\n"; exit(1); } if(n > 0) { return (power(x,n-1)*x); } else { // n == 0, base case return 1; } }

Binary Search

Easiest method: search through every single element in the array.

Example
#include<iostream>
using namespace std;

// size: size of the array // key : the number to be searched // found : true if the key is found, false otherwise // location: location of the key in the array if the key is found void search(const int a[], int size, int key, bool& found, int& location); int main() { int a[] = {2,3,5,7,11,13,19,23,29,31,37,41,43,47}; int size = 14; int key, location; bool found; cout << "Enter a number to be located: "; cin >> key; search(a, 14, key, found, location); if(found) { cout << key << " is in index location " << location << endl; } else { cout << key << " is not in the array." << endl; } return 0; } void search(const int a[], int size, int key, bool& found, int& location) { found = false; for(int i = 0; i < size; i++) { if(key == a[i]) { found = true; location = i; return; } } }

Output
Enter a number to be located: 32
32 is not in the array.

Output
Enter a number to be located: 31
31 is in index location 9
#include<iostream>
using namespace std;

// find the key from a[first] to a[last] // a[] : array, in ascending order // first: index // last: index // key : the number to be searched // found : true if the key is found, false otherwise // location: location of the key in the array if the key is found void search(const int a[], int first, int last, int key, bool& found, int& location); int main() { int a[] = {2,3,5,7,11,13,19,23,29,31,37,41,43,47}; int size = 14; int key, location; bool found; cout << "Enter a number to be located: "; cin >> key; search(a, 0, 13, key, found, location); if(found) { cout << key << " is in index location " << location << endl; } else { cout << key << " is not in the array." << endl; } return 0; } void search(const int a[], int first, int last, int key, bool& found, int& location) { int mid; if(first > last) { found = false; } else { mid = (first + last)/2; if(key == a[mid]) { found = true; location = mid; } else if (key < a[mid]) { search(a, first, mid - 1, key, found, location); } else if (key > a[mid]) { search(a, mid + 1, last, key, found, location); } } }