Wyo C++                                                                            Name -
Ch. 18 Worksheet #2

Answer the following on lined paper.

1. Compute mystery1(20) where mystery1 is the following recursive function.

double mystery1(int num)
{
     if (num > 0)
         return ( num + mystery1(num - 1));
     else
         return 0;
}

2. Rewrite mystery1 as an iterative function (i.e. one that uses a loop) as simply and efficiently as possible.

int mystery1(int num)
{

3. Write a recursive function named fib that calculates an individual term in the Fibanocci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. The function receives one parameter which indicates which term of the sequence the function is to return. For example, the function call      fib(7)     evaluates to 8 since 8 is the seventh term of the Fibonacci sequence.

int fib(int term)
{
     // precondition: 0 < term < INT_MAX
     // postcondition: the term"th" term of the Fibonacci
     //      sequence is returned. See the example above.