next up previous contents index Search
Next: 0.5.2 Permutations Up: 0.5.1.2 Linear Solution Previous: 0.5.1.2 Linear Solution

0.5.1.2.1 Source Code


#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);
}

Scott Gasch
1999-07-09