Gate CS-2008 Question Paper With Solutions

Q. 17 Consider the following C program that attempts to locate an element x in an array Y [ ]
using binary search. The program is erroneous.

1. f(intY[10],intx){
2. inti,j,k;
3. i=0;j=9;
4. do {
5. k=(i+j)/2
6. if (Y[K]<x)i=k;elsej=k;
7. } while ((Y[k]!=x)&&(i<j)),
8. if (Y[k]==x) print f(“x is in the array”);
9. else printf(“x is not in the array”);
10. }

The correction needed in the program to make it work properly is

(A) change line 6 to : if (Y[k]) < x) i = k + 1;else j = k – 1;

(B) change line 6 to: if (Y[k] < x) i = k – 1;else j = k + 1;

(C) change line 6 to: if (Y[k] < x) i = k; else j = k;

(D) change line 7 to : } while ((Y[k] == x)&&( i < j));

Answer: (A)

Explanation:

Gate CS-2008 Question Paper With Solutions

Learn More:   Gate ME-2003 Question Paper With Solutions

LEAVE A REPLY

Please enter your comment!
Please enter your name here