10. Consider the following function: int test(int x, int y) { if (x == y) return
ID: 3758464 • Letter: 1
Question
10. Consider the following function:
int test(int x, int y)
{
if (x == y)
return x;
else if (x > y)
return (x + y);
else
return test(x + 1, y - 1);
}
What is the output of the following statements?
a. cout << test(5, 10) << endl;
b. cout << test(3, 9) << endl;
I ran the program and got the following: 15 and 6
AND
11. Consider the following function:
int func(int x)
{
if (x == 0)
return 2;
else if (x == 1)
return 3;
else
return (func(x - 1) + func(x - 2));
}
What is the output of the following statements?
a. cout << func(0) << endl;
b. cout << func(1) << endl;
c. cout << func(2) << endl;
d. cout << func(5) << endl;
I would like to understand how these two codes exactly work. For example, why the second one gives 2, 3, 5, 21 instead of 2, 3, 1, 8?
Explanation / Answer
10.a.
cout << test(5, 10) << endl;
When we walk thru the function test(x,y):
x=5 y=10
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(5+1,10-1)
=> return test(6,9)
In the same manner
x=6 y=9
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(6+1,9-1)
=> return test(7,8)
In the same manner
x=7 y=8
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(7+1,8-1)
=> return test(8,7)
In the same manner
x=8 y=7
Here x is not equal to y
and x is greater than y =>return(x+y)
=>return(8+7)
=> 15
+++++++++++++++++++++
10.b.
cout << test(3, 9) << endl;
When we walk thru the function test(x,y):
x=3 y=9
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(3+1,9-1)
=> return test(4,8)
In the same manner
x=4 y=8
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(4+1,8-1)
=> return test(5,7)
In the same manner
x=5 y=7
Here x is not equal to y
and x is not greater than y
so control goes to else part
return test(x + 1, y - 1) => return test(5+1,7-1)
=> return test(6,6)
In the same manner
x=6 y=6
Here x is equal to y => return(x)
=> return(6)
=> 6
+++++++++++++++++++++++++++++++++++++++
11.a.
cout << func(0) << endl;
consider func(0)
Here x=0
When we walk thru the function:
x == 0 is true => return 2
=> 2
+++++++++++++++++++++++++++++++++++++++
11.b.
cout << func(1) << endl;
consider func(1)
Here x=1
When we walk thru the function:
x == 0 is false
x == 1 is true => return 3
=> 3
+++++++++++++++++++++++++++++++++++++++
11.c.
cout << func(2) << endl;
consider func(2)
Here x=2
When we walk thru the function:
x == 0 is false
x == 1 is false
Control comes to else => return (func(x - 1) + func(x - 2));
=> return (func(2 - 1) + func(2 - 2))
=> return (func(1)+func(0))
=> return (3 + 2) //from the results of 11.b. and 11.a.
=> return 5
=> 5
+++++++++++++++++++++++++++++++++++++++
11.d.
cout << func(5) << endl;
consider func(5)
Here x=5
When we walk thru the function:
x == 0 is false
x == 1 is false
Control comes to else => return (func(x - 1) + func(x - 2));
=> return (func(5 - 1) + func(5 - 2))
=> return (func(4)+func(3))
//from results below func(3)=8 amd func(4)=13
=> return (13 + 8)
=> return 21
=> 21
consider func(3)
Here x=3
When we walk thru the function:
x == 0 is false
x == 1 is false
Control comes to else => return (func(x - 1) + func(x - 2));
=> return (func(3 - 1) + func(3 - 2))
=> return (func(2) + func(1))
=> return (5 + 3) //from the results of 11.c. and 11.b.
=> return 8
=> 8
consider func(4)
Here x=4
When we walk thru the function:
x == 0 is false
x == 1 is false
Control comes to else => return (func(x - 1) + func(x - 2));
=> return (func(4 - 1) + func(4 - 2))
=> return (func(3) + func(2))
//from above results func(3) =8 and func(2) =5
=> return (8 + 5)
=> return 13
=>13
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.