To Find the maximum consecutive sum of integers in an array
algorithm, arrays, c
Solution
You are not using the correct indexes:
see here for demo : http://codepad.org/wbXZY5zP
int max_sum, temp_sum, i, n = 8, t;
temp_sum = max_sum = a[0];
for (i = 0; i < n; i++) {
(...)
}
Problem
I have this implementation, the result of this program is 100 but the correct answer is 103. is anyone knows what is wrong in this implementation or if there is a better way for Finding the maximum consecutive sum of integers in an array? Thanks in advance. ``` #include <stdio.h> int main(void) { int a[] = { -3, 100, -4, -2, 9, -63, -200, 55 }; int max_sum, temp_sum, i, n = 12, t; temp_sum = max_sum = a[0]; for (i = 1; i < n; i++) { if (a[i] > 0) temp_sum += a[i]; else { t = 0; while (a[i] < 0 && i < n) { t += a[i]; i++; } if (temp_sum + t > 0) { temp_sum = temp_sum + t + a[i]; if (temp_sum > max_sum) max_sum = temp_sum; } else if (i < n) temp_sum = a[i]; } } if (temp_sum > max_sum) max_sum = temp_sum; printf("Maximum Numbers is %d \n", max_sum); return 0; } ```