Write a C program, containing the following functions.
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
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;
}
Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...
1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. Write a recursive C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number.
Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.). # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...
In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...
c++ program Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
C++ programming terms please The following program computes value of 6th Fibonacci number: int f(int n) { if (n= =1 | n= = 2) return 1; return f(n-1) + f(n-2); } int main () { cout < < f(6) << endl; }
(a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....
Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n using //recursion. Note: 2^3 = 2 * 2^2 . 2^2 = 2 * 2^1 2^1 = 2 What value/values of n will stop the recursion (base case)? Sample output: 5 2 to 5 is 32
13. Write a recursive function with the declaration: int count Equal (int* numbers, int n, int x) that has as parameters an array numbers with n > 0 elements, and an integer x, and returns how many times I appears in the array. 14. Write a recursive function with the declaration: double dist(double* u, double *v, int n) that gets two double precision arrays with n > 1 elements and returns the value: Vu[0] - v[0])2 + (u[1] – v[1])2...
a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...
All in C++ Create a function NumEven( std: forward_list-int> SList) that computes and returns the number of even values in a singly linked list SList of type std: forward_list-int> If there is no even value or the list is empty, return 0. Select the appropriate C++ statement for each of the below to implement the function. unsigned NumEven (std:: forward_list<int> Slist) { Select] Select ] Select ] } [ Select] for (int i; i < num; i++) { if (n...