Question
i cant get the equations right to get the proper outcome Code: #include #include int *low_address; int *high_address; int recursive_fibonacci(int n){ if (n
i cant get the equations right to get the proper outcome
Code:
#include
#include
int *low_address;
int *high_address;
int recursive_fibonacci(int n){
if (n <= 1)
return n;
return recursive_fibonacci(n-1) + recursive_fibonacci(n-2);
}
int iterative_fibonacci(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;
}
int main(){
int i = 0, n = 40;
clock_t t1, t2;
int m1,m2;
double time_span1, time_span2;
high_address = &i;
low_address = high_address;
printf("iterative_fibonacci(%d): %d ", n, iterative_fibonacci(n));
printf("high address: %lu ", high_address);
printf("low address: %lu ", low_address);
int memory_span1 = (unsigned long) high_address - (unsigned long) low_address;
printf("memory span: %d ",memory_span1);
m1 = 500000;
t1=clock();
for (i=0; i< m1; i++) {
iterative_fibonacci(n);
}
t2=clock();
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: ");
printf("iterative_fibonacci(40): %d ",iterative_fibonacci(40));
printf("high address:%d ");
printf("low address:%d ");
printf("memory span:%d ");
m2 = 500000;
t2=clock();
for (i=0; i< m2; i++) {
recursive_fibonacci(n);
}
t2=clock();
time_span2 = (double) t2+t1;
printf("time_span(iterative_fibonacci(%d) for %d times): %0.1f (ms) ", n, m1, time_span1);
printf(" Comparison of recursive and iterative algorithms: ");
int memory_span2 = (unsigned long) high_address + (unsigned long) low_address;
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;
}
Required Outcome:
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
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