The GATE Grind

GATE 2014 CS (CS2) – Question 50

Programming and Data Structures · Recursion · 2 marks · Numerical answer

Consider:

double f(double x) {
if (abs(x*x - 3) < 0.01) return x;
else return f(x/2 + 1.5/x);
}

Give q to two decimals such that f(q) returns q.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 1.72 to 1.74

Explanation

The update x/2+1.5/x is Newton’s iteration for x²=3. Taking **q=1.73** gives q²=2.9929 and |q²−3|=0.0071<0.01, so the function returns q immediately. The printed abs name is normally the integer C function; the intended floating absolute value is fabs. The chosen q satisfies either interpretation.