Consider the following program: int f(int n); int main() { cout << \"The value o
ID: 3873528 • Letter: C
Question
Consider the following program:
int f(int n); int main() { cout << "The value of f(8) is " << f(8) << endl; return 0; } // end main /** @pre n >= 0. */ int f(int n) { cout << "Function entered with n = " << n << endl; switch (n) Exercises 91 { case 0: case 1: case 2: return n + 1; default : return f(n-2) * f(n-4); } // end switch } // end f
Show the exact output of the program. What argument values, if any, could you pass to the function f to cause the program to run forever?
Explanation / Answer
Below are the answers don't forget to upvote if you liked my solution.
A) Output for f(8):
Function entered with n = 8
Function entered with n = 6
Function entered with n = 4
Function entered with n = 2
Function entered with n = 0
Function entered with n = 2
Function entered with n = 4
Function entered with n = 2
Function entered with n = 0
The value of f(8) is 27
B) The program will run forever for f(7) , because of the default condition f(n-2) * f(n-4) , in this f(n-4) it will never return.
sample output (trimmed output):
Function entered with n = 7
Function entered with n = 5
Function entered with n = 3
Function entered with n = 1
Function entered with n = -1
Function entered with n = -3
Function entered with n = -5
Function entered with n = -7
Function entered with n = -9
Function entered with n = -11
Function entered with n = -13
Function entered with n = -15
Function entered with n = -17
Function entered with n = -19
Function entered with n = -21
Function entered with n = -23
Function entered with n = -25
Function entered with n = -27
Function entered with n = -29
Function entered with n = -31
Function entered with n = -33
Function entered with n = -35
Function entered with n = -37
Function entered with n = -39
Function entered with n = -41
Function entered with n = -43
Function entered with n = -45
Function entered with n = -47
Function entered with n = -49
Function entered with n = -51
Function entered with n = -53
Function entered with n = -55
Function entered with n = -57
Function entered with n = -59
Function entered with n = -61
Function entered with n = -63
Function entered with n = -65
Function entered with n = -67
Function entered with n = -69
Function entered with n = -71
Function entered with n = -73
Function entered with n = -75
Function entered with n = -77