Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help getting the correct output Code (in c): #include #include int *low_address; int *high_address; int recursive_fibonacci(int n){ if (n

i need help getting the correct output

Code (in c):

#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 Output:

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

Students also viewed these Databases questions

Question

What are the main differences between rigid and flexible pavements?

Answered: 1 week ago

Question

What is the purpose of a retaining wall, and how is it designed?

Answered: 1 week ago

Question

How do you determine the load-bearing capacity of a soil?

Answered: 1 week ago

Question

what is Edward Lemieux effect / Anomeric effect ?

Answered: 1 week ago