Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider this recursive function: int foo(int N){ if (N = res 2) return res1; else return res2; } 1* Write the recurrence formula for the

Consider this recursive function:

int foo(int N){ if (N <= 1) return 5; int res1 = 3*foo(N/2); int res2 = foo(N-1); if (res1 >= res 2) return res1; else return res2; } 

1* Write the recurrence formula for the TIME COMPLEXITY of this function, including the base cases for N>=0. (You do NOT need to solve it.) (Note that it should NOT be the formula for what the function computes, but for how long it takes.)

2* Draw the tree that shows the function calls performed in order to compute foo(5) (the root will be foo(5) and it will have a child for each recursive call.) Also show what each call returns by using an arrow pointing back from the child to the parent.

3* Write the memoized version of this function in a file called memoization.c. 4* Re-implement this function with memoization (i.e. use a solution array to look-up and store results of recursive calls). The function signature can be whatever you want. 5* Write a wrapper function that calls the memoized foo function. The signature should be: int foo_wrapper(int N) . 6* The main function should call and print the result for foo_wrapper(15) and then for foo_wrapper(10) and after that it should repeatedly read N from the user and call foo_wrapper(N) and print the result until the user enters -1 for N.

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

More Books

Students also viewed these Databases questions

Question

What is the growth rate of GDP per capita?

Answered: 1 week ago