a. In the Test() function the base case is Test(0). Beacuse a base case is that in which no recursion is occured . it's value is predefined. Here the value of Test(0) is already defined that is 0
b. A general case is that in which recursion is called and on calling recursion it is going close to the base case. Here in Test() function every Positive integer is a general case.Because on every call of Test() function the int number is reducing with one. And at last it is becoming Zero. which is the base case. For ex. Test(5), Test(8)
c. Test(0) is a valid call beacuse the value of Test(0) is 0 which is predefined is the function. As I already mention is section a of the same question.
d. Test(5) is a valid call, it's value is 15. Here is the explanation:
Test(5) -> 5+Test(4) -> 5+4+Test(3) -> 5+4+3+Test(2) -> 5+4+3+2+Test(1) -> 5+4+3+2+1+Test(0) -> 5+4+3+2+1+0 -> 15
e. Test(-2) is a invalid call. Beacuse on every recursion it is going far from the base case and at last it will give a segmentation fault or memory error. Here's the explanation: Test(-2) -> -2+ Test(-3) -> (-2)+(-3)+Test(-4) -> (-2)+(-3)+(-4)+Test(-5) -> ... like this. it is a never ending process. So Test(-2) is a invalid call.
Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2...
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...
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...
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...
on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s.charAt(last) == 0) { return 2 - test(s, last - 1); return 1 + 2 test(s, last - 1); What is the output of: test("11001", 4). TTT Arial 3 (12pt) - TEE 25
C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378 should be printed as 2 3 7 8 Be sure to test your program with numbers of different length. The recursive function should return an int and take an int as a parameter. The function should have a base case where the parameter is 0 and this should return 0 Define a temp variable using the remainder operator where temp is equal to the...
CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...
2. Consider the following recursive function (Chapter 17, #9, modified) void recFun (int x) { if (x > 10) { recFun (x / 10); cout<< X % 10; } else cout<< x; } a) What is printed by the call: recFun (268); b) How can the code be modified so that when a number is input, its digits are printed on separate lines? c) How can the code be modified so that the sum of all digits is printed?
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1) Prod = M; //base case else Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...
Given the following recursive function: unsigned int mymod( unsigned int a, unsigned int b ) { if( a < b ) return a; return mymod( b - a, b ); } the function call mymod(5, 2) will Select one: a. execute infinitely and terminates with an error. b. execute until some if-condition matches. c. not compile, it will generate a compilation error. d. return the value: 1
This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);