Question
fi.c */ #include #include #include fibonacci.h int *la; int *ha; int main(){ int i=0, n = 40; clock_t t1, t2; printf(**Iterative algorithm measurement** ); ha
fi.c
*/
#include
#include
#include "fibonacci.h"
int *la;
int *ha;
int main(){
int i=0, n = 40;
clock_t t1, t2;
printf("**Iterative algorithm measurement** ");
ha = &i;
la = ha;
printf("iterative_fibonacci(%d):%d ", n, iterative_fibonacci(n));
printf("high address:%lu ", ha);
printf("low address:%lu ", la);
int memory_span1 = (unsigned long) ha - (unsigned long) la;
printf("iterative_fibonacci(%d) memory span:%d ", n, memory_span1);
//run time measuring for iterative_fibonacci
int m1 = 500000;
t1=clock();
for (i=0; i
iterative_fibonacci(n);
}
t2=clock();
double time_span1 = (double) t2-t1;
printf("time_span(iterative_fibonacci(%d) for %d times):%0.1f (ms) ", n, m1, time_span1);
printf(" **Recursive algorithm measurement** ");
la = ha;
printf("recursive_fibonacci(%d):%d ", n, recursive_fibonacci(n));
printf("high address:%lu ", ha);
printf("low address:%lu ", la);
int memory_span2 = (unsigned long) ha - (unsigned long) la;
printf("recursive_fibonacci(%d) memory span:%d ", n, memory_span2);
//run time measuring recursive_fibonacci
int m2 = 10;
t1=clock();
for (i=0; i recursive_fibonacci(n); } t2=clock(); double time_span2 = t2-t1; printf("time_span(recursive_fibonacci(%d) for %d times) in (ms):%0.1f ", n, m2, time_span2); printf(" **Comparison of recursive and iterative algorithms** "); printf("memory_span(recursive_fibonacci(%d))/memory_span(iterative_fibonacci(%d)):%0.1f ", n, n, ((double) memory_span2)/memory_span1); printf("time_span(recursive_fibonacci(%d))/time_span(iterative_fibonacci(%d)):%0.1f ", n, n, (time_span2/time_span1)*(m1/m2)); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started