forked from embedded2015/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterative.c
45 lines (39 loc) · 811 Bytes
/
iterative.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_ARR 100
int maxSubArray(int A[], int size);
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
int A[MAX_ARR];
int i = 0;
while(fscanf(fp, "%d", &A[i]) != EOF) {
printf("%d ", A[i]);
i++;
}
fclose(fp);
int max;
printf("\n%d\n", i);
max = maxSubArray(A, i);
printf("%d\n", max);
return 0;
}
int maxSubArray(int A[], int size)
{
int i, j, k;
int temp = 0;
int sum = INT_MIN;
for(i = 0; i < size; i++) {
for(j = 0; j < size - i; j++) {
for(k = j; k < j + i; k++)
temp += A[k];
if(temp > sum)
sum = temp;
printf("%d ", temp);
temp = 0;
}
}
return sum;
}