CS312 Lab 9: Fibonacci Numbers and Recursion

Due Monday, April 8, at 5:00 PM

Fibonacci numbers are generally defined like this:
fib(n) = fib(n-1) + fib(n-2)

Modify the fib.cpp example from class today so that it will calculate recursive numbers without using multiple calls. This requires saving the fib(n-2) value that is calculated as part of calculating fib(n-1), so that it doesn't need to be calculated separately. This would be rather easy in Python, but will require a little more creativity in C++. Resist the temptation to just convert the whole thing to an iterative solution.
After finishing your solution, compile it with optimization on (-O3 for g++, may vary in other environments) and figure out if the compiler has applied a tail recursion optimization. For gdb, run "gdb a.out" (or whatever your binary's name is), then "disas functionname" with whatever your recursive function's name is. For other platforms, the exact steps may vary a bit, but generally speaking, the same process applies. gdb will be happy to disassemble a clang-produced binary, for example.