edited by
18,192 views
58 58 votes

 The following function computes the maximum value contained in an integer array $P[ \ ]$ of size $n$ $(n>=1)$.                  

int max (int *p,int n) {
    int a = 0, b=n-1;
    
    while (__________) {
        if (p[a]<= p[b]) {a = a+1;}
        else             {b = b-1;}
    }
    return p[a];
}

The missing loop condition is:

  1.  $a\ \ != n$ 
  2.  $b\ \ != 0$ 
  3.  $b>(a+1)$ 
  4.  $b\ \ != a$ 

3 Answers

Best answer
90 90 votes

Answer is (D).

Hint : Given in the question itself that we start comparing the contents of an array  from $a[0]$ and $a [n-1]$ (converging from both side) then condition must be till both meet at a point and that point will be $a=b$.
Hence loop condition should be $a!=b$.

Option C fails for $n=2, p = [1, 2].$

edited by
Answer:
Position:
Show:

Related questions

72 72 votes
12 answers 12 answers
25.1k
25.1k views
Sandeep Singh asked Feb 12, 2016
25,145 views
What will be the output of the following $C$ program?void count (int n) { static int d=1; printf ("%d",n); printf ("%d",d); d++; if (n>1) count (n-1); printf ("%d",d); } ...
94 94 votes
5 answers 5 answers
30.2k
30.2k views
Sandeep Singh asked Feb 12, 2016
30,246 views
Consider the following C program.# include <stdio.h void mystery (int *ptra, int *ptrb) { int *temp; temp = ptrb; ptrb =ptra; ptra = temp; } int main () { int a...
85 85 votes
4 answers 4 answers
20.1k
20.1k views
Sandeep Singh asked Feb 12, 2016
20,061 views
Consider the following "C" program.void f(int, short); void main() { int i = 100; short s = 12; short *p = &s; ____________; // call to f() } Which one of the following e...
66 66 votes
6 answers 6 answers
27.7k
27.7k views
Sandeep Singh asked Feb 12, 2016
27,713 views
Consider the transition diagram of a PDA given below with input alphabet $\Sigma=\{a,b\}$ and stack alphabet $\Gamma = \{X,Z\}$. $Z$ is the initial stack symbol. Let $L$ ...