Question

Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

#include<stdio.h>
#include <sys/time.h>
int fib_recursive(int n)
{
if (n <= 1)
return n;
return fib_recursive(n-1) + fib_recursive(n-2);
}
  
int fib_iterative(int n)
{
int a = 0, b = 1, c, i;
if( n == 0)
return a;
for (i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}

// function to return difference between two time in milliseconds
float timedifference_msec(struct timeval t0, struct timeval t1)
{
return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}
  
int main ()
{
// delacring struct of type timeval to store current time
struct timeval t0;
struct timeval t1;
// delcaring float variables to store time difference in float format.
float elapsed_iter,elapsed_recur;
int i;
//print data as asked in the question
printf("Iterative algorithm measurement:\n");
printf("iterative_fibonacci(40): %d\n",fib_iterative(40));
printf("high address:%p\n",fib_iterative(40));
printf("low address:%d\n");
printf("memory span:%d\n");
// library function called to get the current time instant.
gettimeofday(&t0, 0);
// running the fibo iterative function for 500000 times.
for(i=0;i<500000;i++)
fib_iterative(40);
// library function called to get the current time instant after execution.
gettimeofday(&t1, 0);
// function called to calculate the time difference in ms
elapsed_iter = timedifference_msec(t0, t1);

printf("time_span(iterative_fibonacci(40) for 500000 times): %f (ms)\n", elapsed_iter);
printf("\n");
printf("Recursive algorithm measurement:\n");
printf("iterative_fibonacci(40): %d\n",fib_iterative(40));
printf("high address:%d\n");
printf("low address:%d\n");
printf("memory span:%d\n");
// library function called to get the current time instant.
gettimeofday(&t0, 0);
// running the fibo recursive function for 10 times.
for(i=0;i<10;i++)
fib_recursive(40);
// library function called to get the current time instant.
gettimeofday(&t1, 0);
  
// function called to calculate the time difference in ms

elapsed_recur = timedifference_msec(t0, t1);

printf("time_span(recursive_fibonacci(40) for 10 times): %f (ms)\n", elapsed_recur);
printf("\n");
printf("Comparison of recursive and iterative algorithms:\n");
printf("memory_span(recursive_fibonacci(40))/memory_span(iterative_fibonacci(40)): %f\n");
printf("time_span(recursive_fibonacci(40))/time_span(iterative_fibonacci(40)): %f\n",(elapsed_recur/elapsed_iter));
// getchar();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT