311 views
0 votes
0 votes
Consider the following C function in which size is the number of elements in the array E:
int MyX(int *E, unsigned int size){
    int Y = 0;
    int Z;
    int i, j, k;
    for(i=0;i<size;i++)
        Y=Y+E[i];
    for(i=0;i<size;i++)
        for(j=i;j<size;j++){
            Z=0;
            for(k=i;k<=j;k++)
                Z=Z+E[k];
            printf("%d ",Z);
            if(Z>Y)
                Y=Z;
        }
    return Y;
}
The value returned by the function MyX is the
A) maximum possible sum of elements in any sub-array of array E
B) maximum element in any sub-array of array E
C) sum of the maximum elements in all possible sub-arrays of array E
D) the sum of all the elements in the array E

I found the answer of this question (A) in every book that I read, But if you see the code Y is calculated in first loop itself and it never changes in the second loop because the condition is if(Z>Y), this if statement never gets executed because max value of Z can be sum of all the elements of array and value of Y is already sum of all the elements of array. So, the second looping statement doesn't change the value of Y, Y is from first looping statement which is sum of all elements of the array E. So option (D) should be the correct answer.

Please correct me if I am wrong and please explain with reasons. Thanks.

Please log in or register to answer this question.

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 2 days ago
45 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
102 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
251 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how