Consider the following C function definition. int Trial(int a, int b, int c) {…

1999

Consider the following C function definition.

int Trial(int a, int b, int c)
{
   if ((a >= b) && (c < b))
       return b;
   else if (a >= b)
       return Trial(a, c, b);
   else
       return Trial(b, a, c);
}

The function Trial:

  1. A.

    finds the maximum of a, b and c

  2. B.

    finds the minimum of a, b and c

  3. C.

    finds the middle number of a, b and c

  4. D.

    None of the above

Attempted by 37 students.

Show answer & explanation

Correct answer: C

The correct answer is: finds the middle number of a, b and c.

The function keeps rearranging the three arguments until the second argument b is between the other two.

If a < b, the call Trial(b, a, c) swaps the first two arguments so that the first argument becomes at least as large as the second.

Once a >= b, the first condition checks whether c < b. If this is true, then a >= b > c, so b is the middle value and the function returns b.

If a >= b but c is not less than b, then b is not the middle value yet, so the call Trial(a, c, b) moves c into the second position and tries again. Thus the recursive calls permute the arguments until the middle value reaches the second position.

Explore the full course: Gate Guidance By Sanchit Sir