GATE | GATE CS 2008 | Question 84
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(int Y[10], int x) { 2. int i, j, k; 3. i = 0; j = 9; 4. do { 5. k = (i + j) /2; 6. if( Y[k] < x) i = k; else j = k; 7. } while(Y[k] != x && i < j); 8. if(Y[k] == x) printf ("x is in the array ") ; 9. else printf (" x is not in the array ") ; 10. } |
chevron_right
filter_none
On which of the following contents of Y and x does the program fail?
(A) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
(B) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
(C) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
(D) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
Answer: (C)
Explanation: See question 3 of https://www.geeksforgeeks.org/data-structures-and-algorithms-set-21/
Quiz of this Question
Recommended Posts:
- GATE | GATE CS 2008 | Question 67
- GATE | GATE CS 2008 | Question 85
- GATE | GATE CS 2008 | Question 83
- GATE | GATE CS 2008 | Question 68
- GATE | GATE CS 2008 | Question 69
- GATE | GATE CS 2008 | Question 70
- GATE | GATE CS 2008 | Question 71
- GATE | GATE CS 2008 | Question 72
- GATE | GATE CS 2008 | Question 73
- GATE | GATE CS 2008 | Question 74
- GATE | GATE CS 2008 | Question 75
- GATE | GATE CS 2008 | Question 76
- GATE | GATE CS 2008 | Question 77
- GATE | GATE CS 2008 | Question 78
- GATE | GATE CS 2008 | Question 79



