edited by
17,549 views
48 48 votes

Consider the C program given below : 

#include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else sum += a [i];
    }
    if (sum > maxsum) maxsum = sum ;
    printf ("%d\n", maxsum);

}

What is the value printed out when this program is executed?

  1. $9$
  2. $8$
  3. $7$
  4. $6$

5 Answers

Best answer
48 48 votes

Answer is C.

$$\begin{array}{|l|l|l|l|l|} \hline \text{i} &  \text{$A[i]$} & \text{for }\rightarrow\text{ if --- satisfied?}  & \text{maxsum} & \text{sum} \\\hline \text{-} &  \text{-} & \text{-}  & 0 & 0  \\\hline 0 &  \text{$A[0]= 2$} & \text{Yes $(i == 0)$}  & 0 & 2 \\\hline 1 &  \text{$A[1]= -2$} & \text{Yes $(a[i] < 0)$}  & 2 & 0   \\\hline 2 &  \text{$A[2]= -1$} & \text{Yes $(a[i] < 0)$}  & \text{(for $\rightarrow$ if $\rightarrow$ if --- not satisfied)} & 0  \\\hline  3 &  \text{$A[3]= 3$} & \text{No (else executed)}  & \text{(for $\rightarrow$ if $\rightarrow$ if --- not satisfied)} & 3  \\\hline 4 &  \text{$A[4]= 4$} & \text{No (else executed)}  & \text{(for $\rightarrow$ if $\rightarrow$ if --- not satisfied)} & 7 \\\hline  5 &  \text{$A[5]= 2$} & \text{Yes $(a[i] < a[i - 1])$}  & 7 & 2 \\\hline \end{array}$$

[End of for loop]

If (sum (i.e., $2$) $ >$ maxsum (i.e., $7$))   // No

                maxsum $=$ sum;   // Not Executed

printf will output maxsum $= 7$

edited by
43 43 votes
The algorithm is finding the maximum sum of the monotonically increasing continuous sequence of positive numbers in the array. So, output would be $3+4 = 7.$
13 13 votes
first of  all  some  synatax  error  in  above  code segment .  right code given  below

# include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else
                sum += a [i];
            }
            if (sum > maxsum) maxsum = sum ;
            printf ("%d\n", maxsum);
}

 

 

 

ans  is  C .

simply  execute  and  trace  value of  sum and  maxsum for i=0 to 5

i  -->      initially  0   1   2   3   4     5

sum            0     0   2   0   3    7    2

maxsum     0     0   2   2   2   2    7

so  maxsum  =  7
Answer:
Position:
Show:

Related questions

7 7 votes
1 answers 1 answer
6.7k
6.7k views
Ishrat Jahan asked Oct 29, 2014
6,703 views
Early binding refers to a binding performed at compile time and late binding refers to a binding performed at execution time. Consider the following statements:Static sco...
75 75 votes
5 answers 5 answers
20.5k
20.5k views
Ishrat Jahan asked Oct 29, 2014
20,491 views
The function f is defined as follows:int f (int n) { if (n <= 1) return 1; else if (n % 2 == 0) return f(n/2); else return f(3n - 1); }Assuming that arbitrarily large int...
30 30 votes
1 answers 1 answer
11.5k
11.5k views
Ishrat Jahan asked Oct 29, 2014
11,533 views
Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing. ...
30 30 votes
5 answers 5 answers
12.6k
12.6k views
Ishrat Jahan asked Oct 29, 2014
12,557 views
Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping.int i; program main() { i = 10; ...