HW3
–
Recursion
Due: Friday, September 12, 2008 at 11:59pm
Assignment Overview
===================
This assignment serves to reinforce the recent lectures on recursion
(also found in section 3.5 of the textbook).
In this assignment you
will trace a few recursive methods and then write some recursive
methods of your own.
Part 1: Tracing
===============
You will be asked to trace two recursive methods using a variety of
inputs.
First though, we have provided an example (as well as an
acceptable solution) so you know what we are looking for.
A sample question might look as follows:
int example(int arr[], int n) {
if (n==0)
return 0;
else {
int smallResult = example(arr, n - 1);
return smallResult + arr[n-1];
}
}
Write the resulting trace, making sure to state what the
value of returnValue will be once execution is complete.
a.
Assume that the call is as follows:
int[] exArray = {1, 2, 3, 4};
int returnValue = example(exArray, 4);
A solution for the above question might be as follows:
example([1, 2, 3, 4], 4)
smallResult = example([1, 2, 3, 4], 3)
smallResult = example([1, 2, 3, 4], 2)
smallResult = example([1, 2, 3, 4], 1)