## Monday, August 27, 2012

### AdaTutor - Outside Assignment 4

#### Exercise in Recursion

Your fourth Outside Assignment is to write, using recursion, a function specified by

```    function Fib(N : in Positive) return Positive;
```

Fibonacci was a mathematician in the Middle Ages.  The so-called Fibonacci Series is a series of integers.&mnsp; Each number in the series is the sum of the two previous numbers.  The first two Fibonacci numbers are 1.

```  N:    1  2  3  4  5  6   7   8   9  10  11   12   13   14   15   16 ...
Fib(N): 1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987 ...
```

Note that if N = 1 or N = 2, then Fib(N) = 1; otherwise, Fib(N) = Fib(N - 1) + Fib(N - 2).  Writing this function in Ada will be an easy and short assignment.  A test driver is provided in FIBTEST.ADA; a listing is on page 15 of your printed course notes.  As before, if all tests are passed, it displays "Congratulations, you completed the assignment!"  If there's an error, it displays the test case, the answer from your function, and the right answer.

A dummy solution is in FIB.DUM. As before, you shouldn't change the lines highlighted here:

```    -- Dummy solution to Outside Assignment 4    separate (Fibtest)
function Fib(N : in Positive) return Positive is
begin return 4;    end Fib;
```

The steps to follow for Outside Assignment 4 are similar to those of the last two Outside Assignments.  They're in your printed course notes on page 16:

1. Copy FIB.DUM to FIB.ADA to make a copy of the dummy solution.  Also, compile the test driver FIBTEST.ADA. You need do this step only once.
2. Edit FIB.ADA to become your real solution.  You may skip this step the first time through, to see error messages from the test driver.
3. Compile FIB.ADA.  If the compiler finds errors, go back to step 2.
4. Link with the name of the main program Fibtest.  Then execute.  If the test driver displays error messages, go back to step 2.
5. When the message "Congratulations, you completed the assignment!" is displayed, you'll have a chance to compare your solution with ours.