Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following C program that attempt... Start Learning for Free
Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.
f (int Y[10] , int x) {
int u, 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 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
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Consider the following C program that attempts to locate an element x ...
when it is option C the control will continue to iterate as i=8 and j=9;
again and again i will be assigned k which itself equals 8 as 8+9/2 being stored in an integer type variable, will evaluate to 8
For option A, with X=9 , k will take the following values:
  • 4
  • 6
  • 7
  • 8 - y[8] = 9, x found
For option D, with X = 10, k will take the following values: 
  • 4, y[4] = 10, x found
View all questions of this test
Most Upvoted Answer
Consider the following C program that attempts to locate an element x ...
Explanation:

The given program attempts to locate an element x in the array Y[] using binary search. However, the program contains an error that causes it to fail for certain inputs. Let's analyze the different options to identify the case where the program fails.

Option A: Y is [1 2 3 4 5 6 7 8 9 10] and x = 10
In this case, the program will successfully find the element 10 in the array Y[]. The binary search algorithm will correctly identify the middle element as 5, but since 5 is less than 10, it will continue searching in the upper half of the array. Eventually, it will find the element 10 and output "10 is in the array". Therefore, this option does not cause the program to fail.

Option B: Y is [1 3 5 7 9 11 13 15 17 19] and x = 1
In this case, the program will successfully find the element 1 in the array Y[]. The binary search algorithm will correctly identify the middle element as 9, but since 9 is greater than 1, it will continue searching in the lower half of the array. Eventually, it will find the element 1 and output "1 is in the array". Therefore, this option does not cause the program to fail.

Option C: Y is [2 2 2 2 2 2 2 2 2 2] and x = 2
In this case, the program fails to locate the element 2 in the array Y[]. The binary search algorithm will repeatedly identify the middle element as 5, but since 5 is not equal to 2, it will continue searching in the upper or lower half of the array. This will result in an infinite loop, as the condition Y[k] != x will never be satisfied. Therefore, this option causes the program to fail.

Option D: Y is [2 4 6 8 10 12 14 16 18 20] and 2 ≤ x ≤ 20 and x is even
In this case, the program will successfully find any even number between 2 and 20 in the array Y[]. The binary search algorithm will correctly identify the middle element and adjust the search range accordingly until it finds the desired even number. Therefore, this option does not cause the program to fail.

Conclusion:
The program fails when the array Y[] contains all the same elements and the element x being searched for is one of those elements. In option C, the array Y[] contains all 2s and the element being searched for is 2, causing an infinite loop. Therefore, option C is the correct answer.
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Top Courses for Computer Science Engineering (CSE)

Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer?
Question Description
Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer?.
Solutions for Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following C program that attempts to locate an element x in an array Y[ ] using binary search. The program is erroneous.f (int Y[10] , int x) {int u, 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 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 > 2d)Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is evenCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev