GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 40

GATE CSE 2014 Set 2 Q40

Programming in C · Functions & Parameter Passing · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q40 2 marks · NAT
Report issue

Functions & Parameter Passing

Question 40, GATE 2014: Consider the printed function double f(double x){if(abs(x*x-3)<0.01) return x; else return f(x/2+1.5/x);}. Give a value q, to two decimal places, such that f(q) returns q. Preserve the printed abs rather than silently replacing it with fabs.
Tap to enlarge
Answer & solution

Official answer: 1.72 to 1.74

Our answer: 1.73 in the intended real-absolute-value model

1. Set up the reasoning. For immediate return under the intended mathematical absolute-value interpretation, test whether the chosen two-decimal q satisfies abs(q²-3)<0.01. The recursive expression is a Newton update for a square root, but no iteration is needed if the starting value already passes.

2. Derive the result. For mathematical absolute value, immediate return requires|q²−3|<.01. q=1.73 gives q²=2.9929 and error.0071<.01, so it returns immediately (as does−1.73). The positive value is within the official range. Literal C abs takes an int: with declarations in scope, the double difference is truncated before testing, so every q with|q²−3|<1 returns immediately, including 1.50. The source should use fabs for floating absolute value if that is intended; the negative solution is also valid without a positivity restriction. Official1.72 to 1.74 is retained, with these distinct semantics explicit.

3. Check the interpretation. The distinction between mathematical absolute value and C's integer abs is substantive. The latter converts the double argument to int before testing, broadening immediate-return values. Also the source does not require q positive, so -1.73 is mathematically valid despite the positive official interval; retain the existing discrepancy.

Original paper, page 40 · Answer key