Question: Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call).

Write a C program, containing the following functions.

  1. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2, .
  2. Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop).
  3. The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs to call the functions many multiple times, for example, call iterative_fibonacci(40) for 500000 times and call recursive_fibonacci(40) for 10 times.

Compile and run your program, the output should be like the following

public test

 Iterative algorithm measurement: iterative_fibonacci(40): 102334155 high address: 6684268 low address: 6684208 memory span: 60 time_span(iterative_fibonacci(40) for 500000 times): 74.0 (ms) Recursive algorithm measurement: recursive_fibonacci(40): 102334155 high address: 6684268 low address: 6682992 memory span: 1276 time_span(recursive_fibonacci(40) for 10 times): 9143.0 (ms) Comparison of recursive and iterative algorithms: memory_span(recursive_fibonacci(40))/memory_span(iterative_fibonacci(40)): 21.3 time_span(recursive_fibonacci(40))/time_span(iterative_fibonacci(40)): 6177702.7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!