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 ") ;
}
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)) ;
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
Consider the following C program that attempts to locate an element x ...
if( Y[k] < x) then i = k + 1;
if given element that we are searching is greater then searching will be continued upper half of array
otherwise j = k - 1;
lower half.
Take few case in consideration i.e.
1. all elements are same
2. increasing order with no repeatation
3. increasing order with repeatation.
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 an array 'Y' using binary search. However, there is an error in the program that needs to be corrected for it to work properly.

Correcting the program:

To correct the program, we need to fix the error in line 6. The correct condition should be:
if (Y[k] < x)="" i="k" +="" 1;="" else="" j="k" -="" />

Here's the corrected program:

f (int Y[10], int x) {
int i, j, k;
i = 0;
j = 9;
do {
k = (i + j) / 2;
if (Y[k] < />
i = k + 1;
else
j = k - 1;
} while (Y[k] != x && i <=>

if (Y[k] == x)
printf("%d is in the array\n", x);
else
printf("%d is not in the array\n", x);
}

Explanation:

The error in the original program is in line 6, where the condition for the if statement is incorrect. In binary search, we compare the middle element of the array with the target element 'x' and based on the comparison, we update the indices 'i' and 'j'.

The correct condition should be:
if (Y[k] < x)="" i="k" +="" 1;="" else="" j="k" -="" />

Reasoning behind the correction:

- If the middle element 'Y[k]' is less than the target element 'x', it means that 'x' must be present in the right half of the array. Therefore, we update 'i' to 'k + 1' to search in the right half.
- If the middle element 'Y[k]' is greater than the target element 'x', it means that 'x' must be present in the left half of the array. Therefore, we update 'j' to 'k - 1' to search in the left half.

By correcting the condition in line 6, we ensure that the program follows the correct binary search algorithm and searches for the target element 'x' in the array 'Y' accurately.

Conclusion:

The correction needed in the program to make it work properly is to change line 6 to: if (Y[k] < x)="" i="k" +="" 1;="" else="" j="k" -="" 1;="" x)="" i="k" +="" 1;="" else="" j="k" -="" />
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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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 ") ;}The correction needed in the program to make it work properly isa)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)) ;Correct answer is option 'A'. 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