Q5 (25pts) Consider the code:
int foo(int N){
if (N <= 3) return 2;
int res1 = 3*foo(N-4);
int res2 = foo(N-2);
return res1-res2;
}
a) (6 points) 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.
b) (5 points) Draw the tree that shows the function calls performed
in order to compute foo(8) (the root will be foo(8) and it will
have a child for each recursive call.)
c) (4pts) In the tree, show the return values for function calls
(2pts) and fill in the answer below
foo(8) = _____________________________________________ (2 out of 4
pts)
d) (7pts) Write code for foo_mem, the memoized version of foo (use
a solution array to look-up and store results of recursive calls).
Comment [O6]: NOT posted
5
e) (3pts) Write the wrapper function that calls the foo_mem
function.
a)
b)
c)
foo(8) = 3*foo(4) - foo(6)
foo(6) = 3*foo(2) - foo(4)
foo(4) = 3*foo(0) - foo(2) = 3*2 - 2 = 4
foo(6) = 3*2 - 4 = 2
foo(8) = 3*4 - 2 = 10
foo(8) = 10
d)
int foo_mem(int N) {
int arr[N+1];
arr[0] = arr[1] = arr[2] = arr[3] = 2;
for (int i = 4; i <= N; i++)
arr[i] = 3*arr[i-4] - arr[i-2];
return arr[N];
}
e)
#include<iostream>
using namespace std;
int foo_mem(int N) {
int arr[N+1];
arr[0] = arr[1] = arr[2] = arr[3] = 2;
for (int i = 4; i <= N; i++)
arr[i] = 3*arr[i-4] - arr[i-2];
return arr[N];
}
int main() {
cout << foo_mem(8);
return 0;
}
Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int...
2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (n < 2) return 1; else return 2*george ((n+1)/2)+2*george (n/2)+2 george((n-1)/2)+2*george (n/2-1); (c) Design a memoization algorithm to compute george(n) for any given n. You do not have to write C++ code. This algorithm should be much faster than the dynamic programming algorithm. What is its time complexity? 2. Consider the function george (int n) computed by...
PROBLEM 4: Consider the recursive C++ function below: void foo(unsigned int n) { if(n==0) cout << "tick" << endl; else { foo(n-1); foo(n-1); foo(n-1); } } 4.A: Complete the following table indicating how many “ticks” are printed for various parameters n. Unenforceable rule: derive your answers “by hand” -- not simply by writing a program calling the function. n number of ticks printed when foo(n) is called 0 1 2 3 4 4.B:...
Consider the following code: 1. float foo (int a, int b, int c, int d, float e) { 2. float e; 3. if (a == 0) { 4. return 0; 5. } 6. int x = 0; 7. if ((a==b) || ((c == d) && bug(a) )) { 8. x=1; 9. } 10. e = 1/x; 11. return e; 12. } Function bug(a) should return a value of true when passed a value of a=1. Build: • a test suite...
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
help 9. Consider the following code segment: Struct some Int a: Long bi Char c Void foo (struct some s) S.a -21 s.b 1989: Int main0 Struct some thing: Foo (thing) Label the following stack diagram-from the perspective of function foo-each field of the struct should be clearly marked. Assume 8 byte wide words. 0x060 0x068 0x070 0x078 0x080 Ox088 Ox090 0x098 0x100 Ox108 Old base pointer Return address 9. Consider the following code segment: Struct some Int a: Long...
3) [16 points total] Consider the following algorithm int SillyCalc (int n) int i; int Num, answer; if (n <= 4) return n 10; else { Num-SillyCalcl n/4) answer = Num + Num + 10; for (i-2; i<-n-1; ++) answer- answer+ answer; return answer; Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2* for some...
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
(O polnto int fint n) int t, s if (n < 2) teturn 1 fa-1); tfa-2); return s+ +SS CaatloatCala*5 aat $5+s 4a4 $5.+ 554 a taata b. Consider the above code soippet. Assume that the initial call is f(5), deaw the activation tree. (8 points) Briefly explain the tem "Garbage collection" as it relates to compiler construction c. (3 points) (O polnto int fint n) int t, s if (n
3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...
Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...