edited by
808 views
1 votes
1 votes

The question is based on the following program fragment.

f(intY[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?

  1. $Y$ is $[1\;2\;3\;4\;5\;6\;7\;8\;9\;10]$ and $x<10$
  2. $Y$ is $[1\;3\;5\;7\;9\;11\;13\;15\;17\;19]$ and $x<1$
  3. $Y$ is $[2\;2\;2\;2\;2\;2\;2\;2\;2\;2]$ and $x>2$
  4. $Y$ is $[2\;4\;6\;8\;10\;12\;14\;16\;18\;20]$ and $2<x<20$ and $’x’$ is even 
edited by

1 Answer

1 votes
1 votes

For option C, these are the values of i, j and k. After i = 8 and j = 9, the value of k will be 8 and since the updated value of i is still 8, this results in an infinite loop. 

 

i j k
0 9 4
4 9 6
6 9 7
7 9 8
8 9 8
8 9 8

 

Answer:

Related questions

3 votes
3 votes
1 answer
1
admin asked Apr 1, 2020
966 views
The following program fragment printsint i = 5; do { putchar(i+100); printf(“%d”, i ;) } while(i);i5h4g3f2el14h3g2f1e0An error messageNone of the above
1 votes
1 votes
3 answers
2
0 votes
0 votes
0 answers
3
admin asked Aug 28, 2020
311 views
A solution for the differential equation $x’(t) + 2x(t) = \delta(t)$ with initial condition $x(\overline{0}) = 0$$e^{-2t}u(t)$$e^{2t}u(t)$$e^{-t}u(t)$$e^{t}u(t)$
1 votes
1 votes
1 answer
4