b. The process for drawing Activation Tree and Activation Tree when the initial call is f(5) is as follows :-
Activaton Tree -
Process -
I will write the function will are called and assign a number to them for simplicity.
First the function f(n) where n = 5 will be called. - f(5) = Num1
-> Next function f(n-1) where n = 5 will be called. - f(4) = Num2
-> Next function f(n-1) where n = 4 will be called. - f(3) = Num3
-> Next function f(n-1) where n = 3 will be called. - f(2) = Num4
-> Next function f(n-1) where n = 2 will be called. - f(1) = Num5
The function Num5 i.e. f(1) return the value 1 as n<2, which will be stored in s of Num4.
-> Next function f(n-2) where n = 2 will be called. - f(0) = Num6
The function Num6 i.e. f(0) return the value 1 as n<2, which will be stored in t of Num4.
The function Num4 i.e. f(2) return the value s+t = 2 , which will be stored in s of Num3.
-> Next function f(n-2) where n = 3 will be called. - f(1) = Num7
The function Num7 i.e. f(1) return the value 1 as n<2, which will be stored in t of Num3.
The function Num3 i.e. f(3) return the value s+t = 3 , which will be stored in s of Num2.
-> Next function f(n-2) where n = 4 will be called. - f(2) = Num8
-> Next function f(n-1) where n = 2 will be called. - f(1) = Num9
The function Num9 i.e. f(1) return the value 1 as n<2, which will be stored in s of Num8.
-> Next function f(n-2) where n = 2 will be called. - f(0) = Num10
The function Num10 i.e. f(0) return the value 1 as n<2, which will be stored in t of Num8.
The function Num8 i.e. f(2) return the value s+t = 2 , which will be stored in t of Num2.
The function Num2 i.e. f(4) return the value s+t = 5 , which will be stored in s of Num1.
-> Next function f(n-2) where n = 5 will be called. - f(3) = Num11
-> Next function f(n-1) where n = 3 will be called. - f(2) = Num12
-> Next function f(n-1) where n = 2 will be called. - f(1) = Num13
The function Num13 i.e. f(1) return the value 1 as n<2, which will be stored in s of Num12.
-> Next function f(n-2) where n = 2 will be called. - f(0) = Num14
The function Num14 i.e. f(0) return the value 1 as n<2, which will be stored in t of Num12.
The function Num12 i.e. f(2) return the value s+t = 2 , which will be stored in s of Num11.
-> Next function f(n-2) where n = 3 will be called. - f(1) = Num15
The function Num15 i.e. f(1) return the value 1 as n<2, which will be stored in t of Num11.
The function Num11 i.e. f(3) return the value s+t = 3 , which will be stored in t of Num1.
Finally, the function Num1 i.e. f(5) return the value s+t = 8.
The total function calls made to function f(int ) are 15.
c. GARBAGE COLLECTOR :-
Whenever the user creates objects dynamically
with the help of new keyword and malloc()
method etc., the objects are allocated
memory in the heap. In some
programming languages like C and C++ etc. the
user, both creates and destroys
the objects manually. But in some
programming languages like java etc. the user
need not to destroy the objects
as Garbage Collector which is an automatic
memory management feature, destroys the objects which are
unreachable.
Garbage Collection removes the heap
memory of the unreachable objects. It
automatically detects all the unreachable objects in our program
and free the respective heap memory which is
allocated to the objects.
The methods generally used for Garbage collection
are Tracing, Reference Counting and Escape
Analysis. There are many Garbage Collection
algorithms such as Mark and Sweep
algorithm, Cheney's algorithm,
Baker's algorithm, Lieberman's
algorithm etc.
Example of Garbage Collection:-
String Ch = new String('Hello');
//here, the new String object can be reached through the
reference of Ch.
Ch = NULL;
//Now, the String object is
not reachable.
Therefore, the String Object Hello is available for Garbage Collection. In Java, it happens automatically while in C/C++ it is done manually by User.
(O polnto int fint n) int t, s if (n < 2) teturn 1 fa-1); tfa-2);...
3 points) Question Three Consider the context-free grammar S >SS+1 SS 1a and the string aa Give a leftmost derivation for the string. 3 points) (4 poiots) (5 points) (3 points) sECTION IWOLAttcmpt.any 3.(or 2) questions from this.scction Suppose we have two tokens: (1) the keyword if, and (2) id-entifiers, which are strings of letters other than if. Show the DFA for these tokens. Give a nightmost derivation for the string. Give a parse tree for the string i) Is...
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...
Consider the following C+s code (call to addToArray not shown int at] - (1. 2, 3 yoid addr oArray Cint "a) int bt] (1, 2, 3) for Cint i o: i<2; 1++) In the data section, include set the array size as arrsze using S and TYPE The code must include a call to addToArray, passing the appropriate argument(s). Your code MUST be implemented to mimic as close as possible the C++ code, including the local variable b in addTOArray...
How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 12. G[2]3 13. G[3]4 14. int i:-4 15. while (i<n) t 16. if (i mod 20) else ( 20. return G[n] 1. if (n 0) 2. return 1 3. else if (n1) f...
S 334 Final E 6Read the ollowing function and drw the corespodingoti she to the function call aterwards (draw it pisel-by pisel, Lplt ech piset) 5 vold dras (int O Int yo, int sEnd, Int yn int p 2dy -dx: int twoDy = 2 * dy, twoDyMinusDx-2+ (dy-da). int x, y: it (xo > xEnd) t x xEnd; xEnd = x0; else f x = x0; setPixel (x, y); I/ plot point (x, y) while (xxEnd) xt+; if (p <0)...
From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(g(n)). Explain your answer and use recursion tree method. void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode;...
2. Given Amdal's Law as shown below (1-2) Form A: T(0,N) 1 Form B:S(s) = (1-p)+ The 60% of program could be parallellized. Using latest compiler, the serial part of N 1) the program double its performance. If the program running on a 5 processor machine, calculate its whole speedup ? (4 marks) 2) What is the overall speedup if we make 90 % of a program run 10 times faster? (4 marks) V Four parts of a program need...
1. Theorem 4.1 (Master Theorem). Let a 2 1 and b >1 be constants, let f(n) be a function, and let T(n) be defined on the nonnegative integers by the recurrences T(n)- aT(n/b) + f(n) where we take n/b to be either 1loor(n/b) or ceil(n/b). Then T(n) has the following asymptotic bounds. 1. If f(n) O(n-ss(a)-) for some constant e > 0, then T(n) = e(n(a). 2. If f(n) e(n(a), then T(n)- e(nlot(a) Ig(n)). 3. If f(n)-(n(a)+) for some constant...
Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...
For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...