Consider the following function definition.
- int f (int x, int y){
- for (int i=0 ; i<y ; i++ ) {
- x= x + x + y;
- }
- return x;
- }
Which of the following statements is/are TRUE about the above function?
GATE CSE 2024Programming in C › General C Programming ConceptsVerified
(B, D) If the inputs are x = 20 , y = 20 x=20, y=20 x = 20 , y = 20 , then the return value is greater than 2 20 2^{20} 2 20; If the inputs are x = 10 , y = 20 x=10, y=20 x = 10 , y = 20 , then the return value is greater than 2 20 2^{20} 2 20
Trace the loop symbolically. Each iteration maps the current value $t$ to $2t + y$. Starting from $x$, the value after $k$ iterations is $t_k = 2^{k}(x+y) - y$ (verified by induction). Since the loop runs $y$ times, the returned value is $2^{y}(x+y) - y$. Substitute each option's $(x,y)$ and compare with the respective powers of two.
The loop executes exactly $y$ times, each time doubling the running value and adding $y$, giving closed form $2^{y}(x+y) - y$. With $y=20$ the multiplier $2^{20}$ along with a positive, non-trivial coefficient of $(x+y)$ forces the result above $2^{20}$ for both B and D, whereas $y=10$ keeps the result (30710) below $2^{20}$ but above $2^{10}$. Hence only B and D are true.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.