Question: Write a memoized version of the recursive factorial function. with the code: / * This program prints out a Fibonacci series * / #include /

Write a memoized version of the recursive factorial function.
with the code:
/* This program prints out a Fibonacci series */
#include
// Function Declaration
long fib (long num);
void fib2(long num);
long fibMemoized(int num, int result_cache[]);
int cnt=0;
int main (void)
{
// Local Declarations
int result_cache [50];
int seriessize;
printf ("This program prints a Fibonacci series.
");
printf ("How many numbers do you want? ");
scanf ("%d", &seriessize);
int fibn=0;
printf("
");
for(int i =0; i < seriessize; i++)
{
fibn = fib(i);
printf("fib(%d)=%3d number of times %d
", i, fibn, cnt);
cnt =0;
}
if (seriessize <2)
seriessize =2;
printf("
\ITERATIVELY
");
fib2(seriessize);
for (int looper =0; looper <= seriessize; looper++)
result_cache[looper]=-1;
result_cache[0]=1;
result_cache[1]=1;
cnt =0;
printf("
Optimized Fibonacci calls: %d
", seriessize);
for (int looper =0; looper < seriessize; looper++)
{
long temp;
temp = fibMemoized (looper, result_cache) ;
printf("fib(%d) is %ld
", looper, temp);
cnt=0;
}
return 0;
}// main
long fibMemoized(int num, int result_cache[])
{
// If results already calculated
if (result_cache [num]>=0)
return result_cache [num] ;
else
{
// printf ("calling fib(%d)
", num);
result_cache [num]= fibMemoized(num -1, result_cache)+
fibMemoized(num -2, result_cache);
return result_cache [num];
}// fibMemoized
}
/* Calculates the nth Fibonacci number.
Pre num identifies Fibonacci number.
Post returns nth Fibonaci number.
*/
long fib (long num)
{
cnt++;
// Statements
// Base Case
if (num ==0|| num ==1)
return 1;
return (fib (num -1)+ fib (num -2));
}// fib
void fib2(long num)
{
int f1=0, f2=1, t;
// if (num <=2)
printf("%8ld",1);
for (int i =2; i <= num; i++)
{
t = f2;
f2= f1+ f2;
f1= t;
if (f2%4)
printf("%8ld", f2);
else
printf("
%8ld ", f2);
}
return;
}
code for Fibonacci in 3 ways using the code above
(a) Write an iterative version of the factorial function. Use it in a loop in the main function to go from 1 to 20. Display the output in a nicely formatted way. (5 points)
(b) Write an recursive version of the factorial function. Use it in a loop in the main function to go from 1 to 20. Display the output in a nicely formatted way. (5 points)
(c) Write an MEMOIZED recursive version of the factorial function. Use it in a loop in the main function to go from 1 to 20. Display the output in a nicely formatted way. (5 points)
(d) What is the number of steps that are done for factorial(20) by each of the 3 methods. (3 points).
Upload the source code. Upload the images of the execution run. (use a 'clear' theme.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!