ernie.WPI.EDU> cat class17a.C // file class17a.C, cs2223, D97/98 // // Fibonacci sequence using recursive function calls // 09Apr08 NW, First version // includes #include "headers.h" #include "Timer.h" // see Class 2 // definitions #define FIB_MAX 35 // anything larger takes too long // prototype long int fib(int); // Fibonacci sequence // main int main () { Timer *timer; for (int n = 0; n <= FIB_MAX; n++) { timer = new Timer(n); cout << "n = " << n << " fib(n) = " << fib(n) << endl; delete timer; // print out the time cout << endl; } return 0; } // end main() long int fib(int n) // Fibonacci sequence { if (n <= 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); } // end fib() // Copyright 1998 Norman Wittels ernie.WPI.EDU> g++ class17a.C Timer.C ernie.WPI.EDU> a.out n = 0 fib(n) = 0 Process 0 took 0 seconds n = 1 fib(n) = 1 Process 1 took 0 seconds n = 2 fib(n) = 1 Process 2 took 0 seconds n = 3 fib(n) = 2 Process 3 took 0 seconds n = 4 fib(n) = 3 Process 4 took 0 seconds n = 5 fib(n) = 5 Process 5 took 0 seconds n = 6 fib(n) = 8 Process 6 took 0 seconds n = 7 fib(n) = 13 Process 7 took 0 seconds n = 8 fib(n) = 21 Process 8 took 0 seconds n = 9 fib(n) = 34 Process 9 took 0 seconds n = 10 fib(n) = 55 Process 10 took 0 seconds n = 11 fib(n) = 89 Process 11 took 0 seconds n = 12 fib(n) = 144 Process 12 took 0 seconds n = 13 fib(n) = 233 Process 13 took 0 seconds n = 14 fib(n) = 377 Process 14 took 0 seconds n = 15 fib(n) = 610 Process 15 took 0 seconds n = 16 fib(n) = 987 Process 16 took 0 seconds n = 17 fib(n) = 1597 Process 17 took 0 seconds n = 18 fib(n) = 2584 Process 18 took 0 seconds n = 19 fib(n) = 4181 Process 19 took 0 seconds n = 20 fib(n) = 6765 Process 20 took 0 seconds n = 21 fib(n) = 10946 Process 21 took 0 seconds n = 22 fib(n) = 17711 Process 22 took 0.016666 seconds n = 23 fib(n) = 28657 Process 23 took 0 seconds n = 24 fib(n) = 46368 Process 24 took 0.016666 seconds n = 25 fib(n) = 75025 Process 25 took 0.033332 seconds n = 26 fib(n) = 121393 Process 26 took 0.033332 seconds n = 27 fib(n) = 196418 Process 27 took 0.099996 seconds n = 28 fib(n) = 317811 Process 28 took 0.099996 seconds n = 29 fib(n) = 514229 Process 29 took 0.183326 seconds n = 30 fib(n) = 832040 Process 30 took 0.299988 seconds n = 31 fib(n) = 1346269 Process 31 took 0.483314 seconds n = 32 fib(n) = 2178309 Process 32 took 0.74997 seconds n = 33 fib(n) = 3524578 Process 33 took 1.26662 seconds n = 34 fib(n) = 5702887 Process 34 took 1.99992 seconds n = 35 fib(n) = 9227465 Process 35 took 3.31653 seconds ernie.WPI.EDU>