GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 48

Solve the repeated doubling-plus-constant loop

Programming in C · Control Flow & Loop Tracing · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q48 2 marks · MSQ
Report issue

Control Flow & Loop Tracing

Question 48, GATE 2024: The C function f(int x,int y) runs for(int i=0;i<y;i++){x=x+x+y;} and returns x. Which statements hold in the intended integer model? A f(20,10)>2²⁰; B f(20,20)>2²⁰; C f(20,10)<2¹⁰; D f(10,20)>2²⁰. The exponents are 20 and 10, not the decimal numbers 220 and 210.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, D

Approach: Derive a closed form for the loop instead of expanding twenty iterations individually.
1. Let x_k be the value after k iterations, with initial value x_0. Since y is unchanged, x_(k+1)=2x_k+y. Adding y gives x_(k+1)+y=2(x_k+y).
2. Therefore x_k+y=2^k(x_0+y), or x_k=2^k(x_0+y)−y. For the positive inputs listed, the loop performs exactly y iterations.
3. f(20,10)=30×2¹⁰−10=30,710. This exceeds 2¹⁰=1,024 but is below 2²⁰=1,048,576, so both A and C are false.
4. f(20,20)=40×2²⁰−20=41,943,020, and f(10,20)=30×2²⁰−20=31,457,260. Both exceed 2²⁰, making B and D true.
Answer: B and D. This trace assumes an int wide enough for these values, such as a 32-bit signed int. On a narrow implementation signed overflow would invalidate the mathematical trace; the original does not specify int width.

Original paper, page 30 · Answer key