What does this function return if it is called with n = 4?
int f(int n) {
if (n == 0)
return 1;
else
return f(n-1) + 1;
}
Can you show the steps as well please?
n = 4 to calculate f(4). it first calls f(3) to calculate f(3). it first calls f(2) to calculate f(2). it first calls f(1) to calculate f(1). it first calls f(0) so, let's go in bottom-up approach to solve this. f(0) = 1 (base case) f(1) = f(0) + 1 = 1 + 1 = 2 f(2) = f(1) + 1 = 2 + 1 = 3 f(3) = f(2) + 1 = 3 + 1 = 4 f(4) = f(3) + 1 = 4 + 1 = 5 so, f(4) gives a value of 5. Answer: 5
What does this function return if it is called with n = 4? int f(int n)...
What is the output of the following java function when called as f(1,25,25)? Int f(int a, int b, int n){ int mid =(a+b)/2; if ((mid*mid <=n) && (n< (mid+1)*(mid+1)) return mid; else If (mid*mid>n) return f(a,mid-1,n); else return f(mid+1,b,n); } B.) Is the following function tail-recursive? Acker(m,n){ if(m=0) n+1 else if(n=0) Acker(m-1,1) else Acker(m-1,Acker(m,n-1)) }
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...
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...
Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What is the output for each function call (a) Function call h(736). Output: (b) Function call h(12345). Output: Answer options: 16,15 26,25 36,35 46,45
(5 pts) What is the ouput of the following function F, for the call int i = F(3)? int F(int n) { int result; if (n > 20) return 1; else { result = F(2*n) * 2; cout << result << " "; return result; } }
. Determine what is calculated for n = 5: unsigned int F(unsigned int n) { if(n == 0) return 1; return n ∗ F(n − 1); } Write an iterative version of the function in this problem
Given these methods: METHOD math1: public int math1( int n ) { if (n <= 1) { return 1; } // if else { return ( n * 2 ) + math1( n-1 ); } // else } // math1 METHOD math2: public int math2( int n ) { if (n <= 1) { return 1; } // if else { return n + math1( n ) * math2( n/2 ); } // else } // math2 (a) Set up...
1. static int function(int n){ return n * function(n + 1); } If you call the method function(2), the expected output is 3. True False 2. public void push(int new_data) { Node new_Node = new Node(new_data); new_Node.next = head; new_Node.prev = null; if (head != null) head.prev = new_Node; head = new_Node; } The code snippet depicts stack implementation True False 3. A ________ list is a linked data structure that consists of a set of sequentially linked records called...
#include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else /* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */ return (josephus(n - 1, k) + k-1) % n + 1; } // Driver Program to test above function int main() { int n = 14; int k = 2; printf("The chosen place...
C++ What output does the following program produce? int getValue(int a, int b, int n); int main() cout << getValue(1, 7, 7) << endl; return 0; } // end main int getValue(int a, int b, int n) int returnValue = 0; cout << "Enter: a = " < a << " b = " << b << endl; int c = (a + b)/2; if (C* C <= n) return value = c; else returnValue = getValue(a, C-1, n); cout...