Professional Documents
Culture Documents
Introduction To Object-Oriented Programming COMP2011: Function II - Recursion
Introduction To Object-Oriented Programming COMP2011: Function II - Recursion
Object-Oriented Programming
Cecia Chan
Cindy Li
Brian Mak
Think Recursively!
“God Help Those Who Help
Themselves.”
n! = n × (n − 1) × (n − 2) × · · · 2 × 1
+ =
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, . . .
Or, equivalently,
factorial(1) :
1<0 false
1 == 0 false
1 * factorial(0)
factorial(0) :
0<0 false
0 == 0 true
return 1
return 1*1 = 1
return 2*1 = 2
return 3*2 = 6
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Fall 2018) p.12
Factorial Function: Recursive vs. Non-Recursive
int factorial(int n) /* factorial.cpp */
{
if (n < 0) // Error checking
return -1;
else if (n == 0) // Base case; ending case too!
return 1;
else // Recursive case
return n * factorial(n-1);
}
int factorial(int n)
{
// Forget the base case, which is the ending case too!
return n * factorial(n-1);
}
int factorial(int n)
{
// Forget checking if n < 0
if (n == 0)
return 1;
if (n == 1) // Base case #2
return 1;
f(4)
f(3) f(2)
f(1) f(0)
repeated computations
return fn;
}
else
return num_zeros(n/10) + num_zeros(n%10);
}
A B C
Step 1
Step 2
Step 3
Step 4
2nd call
3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
1st call
if (data[mid] == value)
return mid; // Base case #2
return NOT_FOUND;
}
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Fall 2018) p.25
Array and Recursion
an array of size N
=
+
an array of size N−1 last element
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n; // #elements in an array to sum
while (cin >> n)
cout << array_sum(a, n) << endl;
return 0;
}
Question: What happens if you pass a value bigger than the size of
the array size to n?
{ kccecia, lixin, mak }@cse.ust.hk COMP2011 (Fall 2018) p.27
Recursion is Natural