The GATE Grind

GATE 2008 CS – Question 84

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

The following erroneous C program attempts binary search in Y[10]:

f(int Y[10],int x) {
 int i,j,k;
 i=0;j=9;
 do {
  k=(i+j)/2;
  if(Y[k]<x) i=k; else j=k;
 } while((Y[k]!=x)&&(i<j));
 if(Y[k]==x) printf("x is in the array");
 else printf("x is not in the array");
}

On which input does it fail?

  1. Y=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], x<10
  2. Y=[1, 3, 5, 7, 9, 11, 13, 15, 17, 19], x<1
  3. Y=[2, 2, 2, 2, 2, 2, 2, 2, 2, 2], x>2
  4. Y=[2, 4, 6, 8, 10, 12, 14, 16, 18, 20], 2<x<20 and x even

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (C) Y=[2, 2, 2, 2, 2, 2, 2, 2, 2, 2], x>2

Explanation

We are sorry we couldn't write detailed soluition for this, we are working on it!, please consider asking your own AI, its just 2-3 clicks with very minimum effort

More Programming in C questions

All Programming in C questions →