The GATE Grind

GATE 2008 CS – Question 85

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");
}

Which correction makes it work properly?

  1. Change line6 to: if(Y[k]<x) i=k+1; else j=k−1;
  2. Change line6 to: if(Y[k]<x) i=k−1; else j=k+1;
  3. Change line6 to: if(Y[k]<=x) i=k; else j=k;
  4. Change line7 to: } while((Y[k]==x)&&(i<j));

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) Change line6 to: if(Y[k]<x) i=k+1; else j=k−1;

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 →