The following program should print whether the sum of the elements of the array is positive or negative:
#include <stdio.h>
#define ARR_SIZE 5
int main()
{
int array[ARR_SIZE] = {1,-2,3,4,-5};
unsigned sum;
int i;
for(i=0, sum=0; i < ARR_SIZE; i++)
{
sum += array[i];
printf("sum %d\n ", sum);
}
printf("%d\n",sum);
if(sum>-1) printf("non negative\n");
else printf("negative\n");
return 0;
}
The program doesn't do what it is supposed to; it prints 'negative' no matter what array values it receives.
For example, the sum of the array written in the above program is 1, and therefore I expected the following output:
sum 1
sum -1
sum 2
sum 6
sum 1
1
non negative
While the output is:
sum 1
sum -1
sum 2
sum 6
sum 1
1
negative
Why do I get this output?