1. Consider the following recursive function: void recFun(int x) { if (x > 0) {
ID: 3564360 • Letter: 1
Question
1. Consider the following recursive function:
void recFun(int x) { if (x > 0) { cout << x % 10 << " "; recFun(x / 10); } else if (x != 0) cout << x << endl; } What is the output of the following statements? a. recFun(258); b. recFun(7); c. recFun(36); d. recFun(-85);
Consider the following recursive function:
void exercise(int x)
{
if (x > 0 && x < 10)
{
cout << x << " ";
exercise(x + 1);
}
}
What is the output of the following statements?
a. exercise(0);
b. exercise(5);
c. exercise(10);
d. exercise(-5);
3.
Consider the following function:
int test(int x, int y)
{
if (x <= y)
return y - x;
else
return test(x - 1, y + 1);
}
What is the output of the following statements?
a. cout << test(3, 100) << endl;
b. cout << test(15, 7) << endl;
Explanation / Answer
1. Consider the following recursive function:
void recFun(int x)
{
if (x > 0)
{
cout << x % 10 << " ";
recFun(x / 10);
}
else if (x != 0)
cout << x << endl;
}
What is the output of the following statements?
a. recFun(258); since 258 > 0 it print 258%10 i.e 8 and call same function with 258/10 i.e recFun(258/10) = recFun(25)
again 25> 0 it print 25 % 10 i.e 5 and call same function with 25/10 i.e recFun(25/10) = recFun(2)
again 2>0 it print 2%10 i.e 2 and call same function with 2/10 i.e recFun(2/10) = recFun(0)
since 0>0 is false and 0!=0 is false recursion will break here.
so it prints 8 5 2
b. recFun(7); 7>0 it print 7%10 i.e 7 and call same function with 7/10 i.e recFun(7/10) = recFun(0)
since 0>0 is false and 0!=0 is false recursion will break here.
so it prints 7
c. recFun(36); 36> 0 it print 36 % 10 i.e 6 and call same function with 36/10 i.e recFun(36/10) = recFun(3)
again 3>0 it print 3%10 i.e 3 and call same function with 3/10 i.e recFun(3/10) = recFun(0)
since 0>0 is false and 0!=0 is false recursion will break here.
so it prints 6 3
d. recFun(-85); since -85>0 is false and -85!=0 is true it print -85.
Consider the following recursive function:
void exercise(int x)
{
if (x > 0 && x < 10)
{
cout << x << " ";
exercise(x + 1);
}
}
What is the output of the following statements?
a. exercise(0); since 0>0 is false it wont print any thing.
b. exercise(5); since 5>0 and 5<10 it print 5 and call same function with 5+1 i.e 6. exercise(6)
since 6>0 and 6<10 it print 6 and call same function with 6+1 i.e 7. exercise(7)
since 7>0 and 7<10 it print 7 and call same function with 7+1 i.e 8. exercise(8)
since 8>0 and 8<10 it print 8 and call same function with 8+1 i.e 9. exercise(9)
since 9>0 and 9<10 it print 9 and call same function with 9+1 i.e 10. exercise(10)
since 10>0 and 10<10 is false recursion breaks here.
so it prints 5 6 7 8 9
c. exercise(10); since 10>0 and 10<10 is false it wont print any thing.
d. exercise(-5); since -5>0 is false it wont print any thing.
3.
Consider the following function:
int test(int x, int y)
{
if (x <= y)
return y - x;
else
return test(x - 1, y + 1);
}
What is the output of the following statements?
a. cout << test(3, 100) << endl; since 3<=100 is true it returns 100-3 i.e 97
so it prints 97.
b. cout << test(15, 7) << endl; since 15<=7 is false. it goes to else block and call same function with test(15-1, 7+1) test(14,8)
since 14<=8 is false. it goes to else block and call same function with test(14-1, 8+1) test(13,9)
since 13<=9 is false. it goes to else block and call same function with test(13-1, 9+1) test(12,10)
since 12<=10 is false. it goes to else block and call same function with test(12-1, 10+1) test(11,11)
since 11<=11 is true it returns 11-11 i.e 0
so it prints 0.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.