Question

(5 pts) What is the ouput of the following function F, for the call int i...

(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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int F(int n) //n=3
{
int result;
if (n > 20)
return 1;
else
{
result = F(2*n) * 2; //recursive calls, F(6)*2,F(12)*2*2,F(24)*2*2*2,1*2*2*2=8
cout << result << " "; // f(24)*2 = 1*2=2 ,F(12)*2 = 2*2 =4, F(6)*2 = 4*2 =8
return result;
}
}

int main()
{

int i = F(3);
cout<<"\ni="<<i;
   return 0;
}

output:

2 4 8 
i=8
Add a comment
Know the answer?
Add Answer to:
(5 pts) What is the ouput of the following function F, for the call int i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following java function when called as f(1,25,25)? Int f(int a,...

    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)) }

  • C++ 1. What is the final output? 2. Why is 2 the ouput for (int)*c? Please...

    C++ 1. What is the final output? 2. Why is 2 the ouput for (int)*c? Please explain what happens during the line c = c + *p +1; #include <iostream> using namespace std; int main() {    int arr[5] = { 5,2,3,1,4 };    int* p = arr;    cout << (int)*p << endl;    unsigned char* c = (unsigned char*)arr;    p = p + 2;    c = c + *p + 1;       cout << (int)*p...

  • Given the following function: int fun1(int count){ int Num ; for (i = 0; i <...

    Given the following function: int fun1(int count){ int Num ; for (i = 0; i < count; ++i) { cin >> Value; if (i == 0) Num = Value; else if (Value > Num) Num = Value; } return Num ; } What is the output of the following C++ code segment if the following list is entered? (Input list:  5 -15 -90 -2 -60 -30) int Num = 0 , Value, numValues; cin >> numValues; if (numValues > 0) cout...

  • Please show steps and final result. int funcB(int); int funcA(int n) { if (n <= 5)...

    Please show steps and final result. int funcB(int); int funcA(int n) { if (n <= 5) return 7; else return n + funcB(n - 2); } int funcB(int n) { if (n <= 3) return 5; else return n * funcA(n - 2); } int main() { int num = 8; cout << funcB(num); return 0; }

  • 5. (7 pts) What wil display on the output screen after following program is executed? includeciostream...

    5. (7 pts) What wil display on the output screen after following program is executed? includeciostream using namespace std int b 40 int A function(int a) int main (void) int c 7, b 15 cout<cA function (e) <cendla return 6 int A function (int a) int i cout<<b<<endl; if (a>-0) else return i i-ai i--ai Ans5 6. (7 pts) Show what will appear on the output screen after the following program is executed tincludeciostream> using namespace std; void A function...

  • Write the output of the following function assuming it was called with the given function call...

    Write the output of the following function assuming it was called with the given function call a. Call: fun(4, 10); fo void fun (int a, int b) cout << b a << endl; if(a >= b) { 나 cout < "boo\n"; return; fun(a 1, b 1); cout << a << endl;

  • b) Consider the following code. public static int f(int n) if (n == 1) return 0;...

    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...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • What does this function return if it is called with n = 4? int f(int n)...

    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?

  • What is the recurrence of the power2() function? What is its running time? Is it faster...

    What is the recurrence of the power2() function? What is its running time? Is it faster than the iterative way? int power(int base, int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= base; } return result; } int power2(int base, int n) { int result; if (n == 0) return 1; if (n % 2 == 0) // 3^10 { result = power2(base, n / 2); // 3^5 return result...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT