GATE 2008 CS – Question 85
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?
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