#include <stdio.h>
#define MAXSEQ 400
int numbers[MAXSEQ];
int count;
int endat;
void doit(void);
int main(void) {
int i = 0;
int num;
printf("enter the numbers, -99999 to end... \n");
do {
scanf("%d", &num);
numbers[i] = num;
i++;
} while (num != 0);
count = i - 1;
doit();
}
void doit(void) {
int maxsofar = numbers[0];
int maxendhere = numbers[0];
int i, a, b;
for (i = 1; i <= count; i++) {
a = maxendhere + numbers[i];
b = numbers[i];
if (a > b) maxendhere = a; else maxendhere = b;
if (maxendhere > maxsofar) {
maxsofar = maxendhere;
endat = i;
}
}
printf("Max value subseq was %d ending at pos %d\n", maxsofar, endat);
}
|