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))
}
here are the answers..
==================================================================
Program:
==================================================================
import java.io.*;
public class Test
{
public static void main (String[] args)
{
System.out.println(f(1, 25,
25));
}
public static 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);
}
}
==================================================================
Output = 5
==================================================================
Ans: B
Acker(m,n){
if(m=0)
n+1
else if(n=0)
Acker(m-1,1)
else
Acker(m-1,Acker(m,n-1))
}
The given function is tail recursive.
Explanation:
A recursive function is tail recursive when recursive call is the last thing executed by the function.
so in the given function Acker(m-1,Acker(m,n-1)) call is the last statement executed by the function. Hence it's a tail recursive function.
==================================================================
Kindly Check and Verify Thanks..!!!
What is the output of the following java function when called as f(1,25,25)? Int f(int a,...
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);
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?
2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn<2 else f(n-1) + f(n-2) end If m=0 else B. function g(m,n) g(m-1,m'n) C. function h(n) if n 100 else 3 h(n+5) end D. function j(m.n) IT m=n 100 j(m-n,n) 10 j(n,n-m) elseif mn else 2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn
JAVA 5) What is the output of the following code? int a = 70; boolean b = false; if(a >= 70) { System.out.print(1); if(b==true) { System.out.print(2); } } else { System.out.print(3); if(b==false) { System.out.print(4); } } System.out.print(5); 6) What is the output of the code above using these initial values? int a = 43; boolean b = false; 7) The following method is SYNTACTICALLY correct (meaning it will compile). True or false? public boolean method() { int value = 5;...
7. What is the output of the following program? #include <iostream> int f(int n, int & v, int * p) { V = *p; v = y + 1; return n+ (*p); int main() { int n = 10; int m = 20; std::cout << fin, n, & m); a. 30. b. 31. c. 41. d. 42. e. An error occurs. 8. What is the output of the following program? Note that both the signature and the body of the...
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 binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...
int binSearch(int arr[], int lo, int hi, int x) { int q = (low + high) / 2; if(arr[q] <= x && arr[q + 1] > x){ return q + 1; } else if(arr[q] == x){ return binSearch(arr, q + 1, high, x); } else if(arr[q] > x){ return binSearch(arr, low, q - 1, x); } else if(arr[q] < x){ return binSearch(arr, q + 1, high, x); } } Based on the above function, write an iterative algorithm (based on...
(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; } }
Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...