Please send questions to
st10@humboldt.edu .
/*--------------------------------------------------
created by st10 at Mon Nov 17 00:36:39 PST 2003
--------------------------------------------------*/
#include <iostream>
#include <cmath>
using namespace std;
/*--------------------------------------------------
Contract: getNthFib : int -> int
Purpose: Compute the nth Fibonacci number
(where 1 is the 1st, 1 is the 2nd,
2 is the 3rd, 3 is the 4th, etc.)
Examples: getNthFib(0) == -1
getNthFib(1) == 1
getNthFib(2) == 1
getNthFib(3) == 2
getNthFib(8) == 21
--------------------------------------------------*/
int getNthFib(int pos)
{
// make sure pos is a reasonable value; return -1 if not
if (pos < 1)
{
return -1;
}
// FIRST base case...
else if (pos == 1)
{
return 1;
}
// SECOND base case...
else if (pos == 2)
{
return 1;
}
// RECURSIVE case
else
{
return (getNthFib(pos-1) + getNthFib(pos-2));
}
}