Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 fiblong num;
long fibMemoizedint num, int resultcache;
int cnt;
int main void
Local Declarations
int resultcache ;
int seriessize;
printf This program prints a Fibonacci series.
;
printf How many numbers do you want? ;
scanf d &seriessize;
int fibn;
printf
;
forint i ; i seriessize; i
fibn fibi;
printffibdd number of times d
i fibn, cnt;
cnt ;
if seriessize
seriessize ;
printf
ITERATIVELY
;
fibseriessize;
for int looper ; looper seriessize; looper
resultcachelooper;
resultcache;
resultcache;
cnt ;
printf
Optimized Fibonacci calls: d
seriessize;
for int looper ; looper seriessize; looper
long temp;
temp fibMemoized looper resultcache ;
printffibd is ld
looper, temp;
cnt;
return ;
main
long fibMemoizedint num, int resultcache
If results already calculated
if resultcache num
return resultcache num ;
else
printf calling fibd
num;
resultcache num fibMemoizednum resultcache
fibMemoizednum resultcache;
return resultcache 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 num
return ;
return fib num fib num ;
fib
void fiblong num
int f f t;
if num
printfld;
for int i ; i num; i
t f;
f f f;
f t;
if f
printfld f;
else
printf
ld f;
return;
code for Fibonacci in 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 to Display the output in a nicely formatted way. points
b Write an recursive version of the factorial function. Use it in a loop in the main function to go from to Display the output in a nicely formatted way. points
c Write an MEMOIZED recursive version of the factorial function. Use it in a loop in the main function to go from to Display the output in a nicely formatted way. points
d What is the number of steps that are done for factorial by each of the methods. 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
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