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.
Please write code in C++ using recursive function Write a program that computes the sequence of Fibonacci numbers. The formula for generating the next Fibonacci number is: Fn = Fn−1 +Fn−2, where F1 = 1 and F2 = 2. For example, F3 = F2 + F1 = 2 + 1 = 3. You will notice that at some point Fibonacci numbers are too large and they do not fit in type int. This is called the integer overflow. When they...
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
Consider the following recursive definition of a factorial function. int factorial ( int n) { if ( n == 0 || n ==1 ) return 1; else return n * factorial (n-1); } Suppose the function factorial (4) is invoke. Trace through the function call, explicitly show how the factorial function is repeatedly called and what is the value of n in each call. Also show the value returned by each call. Give an...
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...