-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUAS#2_FileTermudahV2.cpp
More file actions
78 lines (68 loc) · 1.51 KB
/
UAS#2_FileTermudahV2.cpp
File metadata and controls
78 lines (68 loc) · 1.51 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
FILE *fp;
struct data{
int angka;
char spasi;
int akhir;
};
int main(){
struct data dt[1001];
fp = fopen("FileTermudahV2.txt", "r");
int angka[1000];
int count = 0;
while(!feof(fp)){
fscanf(fp, "%d%c", &dt[count].angka, &dt[count].spasi);
count++;
}
// for(int i = 0; i < count; i++){
// printf("%d%c", dt[i].angka, dt[i].spasi);
// }
// printf("\n");
int totalBaris = 0;
int idxAngka = 0;
for(int i = 0; i < count; i++){
if(dt[i].spasi == ' '){
idxAngka++;
} else if(dt[i].spasi == '\n' || dt[i].spasi == '\0'){
dt[totalBaris].akhir = idxAngka+1;
totalBaris++;
idxAngka = 0;
}
}
int matrix[totalBaris][1001];
int index = 0;
for(int i = 0; i < totalBaris; i++){
for(int j = 0; j < dt[i].akhir; j++){
matrix[i][j] = dt[index].angka;
index++;
}
}
// for(int i = 0; i < totalBaris; i++){
// for(int j = 0; j < dt[i].akhir; j++){
// printf("%d ", matrix[i][j]);
// }
// printf("\n");
// }
int jumlahPerBaris[1001] = {0};
for(int i = 0; i < totalBaris; i++){
for(int j = 0; j < dt[i].akhir; j++){
jumlahPerBaris[i] += matrix[i][j];
}
}
int max = -1;
int min = 100000001;
for(int i = 0; i < totalBaris; i++){
if(max < jumlahPerBaris[i]){
max = jumlahPerBaris[i];
}
if(min > jumlahPerBaris[i]){
min = jumlahPerBaris[i];
}
}
printf("Max: %d\n", max);
printf("Min: %d\n", min);
int result = max - min;
printf("%d\n", result);
fclose(fp);
return 0;
}