What will be the output of the following pseudocode for a = 2, b = 6?Integer funn(Integer a, Integer b If(a > 0) If(b > 0) Return a + b + funn(a + 1, 0) + funn(a + 2, 0) + funn(a + 3, 0) End If End If Return a + bEnd Function funn()
Question
What will be the output of the following pseudocode for a = 2, b = 6?Integer funn(Integer a, Integer b If(a > 0) If(b > 0) Return a + b + funn(a + 1, 0) + funn(a + 2, 0) + funn(a + 3, 0) End If End If Return a + bEnd Function funn()
Solution
The pseudocode provided is a recursive function, which means it calls itself within its own definition. Here's how it works:
- The function
funnis called witha = 2andb = 6. - Since
a > 0andb > 0, it enters the nested if condition. - It then returns
a + b + funn(a + 1, 0) + funn(a + 2, 0) + funn(a + 3, 0). - The recursive calls
funn(a + 1, 0),funn(a + 2, 0), andfunn(a + 3, 0)are all withb = 0, so they will not enter the nested if condition. Instead, they will each returna + b, whereb = 0. - So,
funn(a + 1, 0) = a + 1 + 0 = 3,funn(a + 2, 0) = a + 2 + 0 = 4, andfunn(a + 3, 0) = a + 3 + 0 = 5. - Substituting these values back into the original return statement, we get
a + b + 3 + 4 + 5 = 2 + 6 + 3 + 4 + 5 = 20.
So, the output of the pseudocode for a = 2 and b = 6 is 20.
Similar Questions
What will be the output of the following pseudocode for a = 4, b = 6?Integer funn(Integer a, Integer b) If(a > 1) Return a * funn (b - 6, a - 4) Else Return 1 End If Return a + b End function funn()Question 79AnswerA.7B.17C.4D.0
What will be the output of the following pseudocode for p = 6, q = 27Integer funn ( integer p, integer q )if ( p> 0 )return p + q + funn ( p - 3, q ) + funn (p-8, q-2 )end if return q - 2end function funn ( )
dyGPTQuestion textWhat will be the output of the following pseudocode for a = 2, b = 6?Integer funn(Integer a, Integer b If(a > 0) If(b > 0) Return a + b + funn(a + 1, 0) + funn(a + 2, 0) + funn(a + 3, 0) End If End If Return a + bEnd Function funn()
What will be the output of the following pseudocode for a = 4 and b = 9?Integer funn(Integer a, Integer b)If(a Return 1 + funn(a, b – 2)End IfIf(a^b >a &b)Return a ^ bElseReturn a &bEnd IfReturn 1End function funn()10-81424
What will be the output of the following pseudocode for a = 5, b = 1?Integer funn(Integer a, Integer b)if((b mod a && a mod b) || (a ^ b > a))a=a^bElseEnd ifreturn a-breturn a+bEnd function funn()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.