Consider the C function given below Assume that the array listA contains n 0 elements sored in ascending order Gate Computer Science 2014

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
See Our team
Wondering how we keep quality?
Got unsolved questions?

Gate-Computer-Science-Engineering-question-paper-2014-->View question

## Consider the C function given below. Assume that the array listA contains n (> 0) elements, sored in ascending order. - Gate Computer Science 2014

int ProcessArray (int * listA, int x, int n)
{
Int 1, j, k;
i = 0;
j = n – 1;
do {
k = (i + j) /2;
if (x < = listA [k])
j = k – 1;
If (listA [k] < = x)
i = k+1;
}while (1 < = j);
If (listA [k] = = x)
return (k) ;
else
return -1;
}
Which one of the following statements about the function ProcessArray is CORRECT?
(A) It will run into an infinite loop when x is not in listA.
(B) It is an implementation of binary search
(C) It will always find the maximum element in listA.
(D) It will return – 1 even when x is present in listA.

Taged users:

Likes:
Be first to like this question

Dislikes:
Be first to dislike this question

It is an implementation of binary search

By the logic of the algorithm it is clear that it is an attempted implementation of
Binary Search. So option C is clearly eliminated. Let us now check for options A and
D.
A good way to do this is to create small dummy examples (arrays) and implement the
algorithm as it is. One may make any array of choice. Running iterations of the
algorithm would indicate that the loop exits when the x is not present. So option A is
wrong. Also, when x is present, the correct index is indeed returned. D is also wrong.
Correct answer is B. It is a correct implementation of Binary Search.

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer

### Watch more videos from this user Here

Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.