• edited by
18,600 views
60 60 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
92 92 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

73 73 votes
12 answers 12 answers
25.7k
25.7k views
Sandeep Singh asked Feb 12, 2016
25,716 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); } ...
96 96 votes
5 answers 5 answers
30.8k
30.8k views
Sandeep Singh asked Feb 12, 2016
30,832 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...
86 86 votes
5 answers 5 answers
20.5k
20.5k views
Sandeep Singh asked Feb 12, 2016
20,494 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...
67 67 votes
6 answers 6 answers
28.3k
28.3k views
Sandeep Singh asked Feb 12, 2016
28,337 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$ ...