#include <stdio.h>
#include <stdlib.h>
void init(FILE* f, int** avrs, int*** lines, int n) {
const int m = 5;
int i;
int j;
int s;
int* as;
int** ls;
int* l;
/* read data */
ls
= malloc(sizeof(int*) * n
); for (i = 0; i < n; i++) {
fscanf(f
, "%d %d %d %d %d%*c", &l
[0], &l
[1], &l
[2], &l
[3], &l
[4]); ls[i] = l;
}
/* compute average */
for (i = 0; i < n; i++) {
l = ls[i];
s = 0;
for (j = 1; j < m; j++) {
s += l[j];
}
as[i] = s / (m - 1);
}
*avrs = as;
*lines = ls;
}
void sort(int* avrs, int** lines, int n) {
int i;
int j;
int t1;
int* t2;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1; j++) {
if (avrs[j] < avrs[j + 1]) {
t1 = avrs[j];
avrs[j] = avrs[j + 1];
avrs[j + 1] = t1;
t2 = lines[j];
lines[j] = lines[j + 1];
lines[j + 1] = t2;
}
}
}
}
void print(int* avrs, int** lines, int n) {
int i;
int* line;
for (i = 0; i < n; i++) {
line = lines[i];
printf("avr:%d %d %d %d %d %d\n", avrs
[i
], line
[0], line
[1], line
[2], line
[3], line
[4]); }
}
void term(int* avrs, int** lines, int n) {
int i;
for (i = 0; i < n; i++) {
}
}
int main(void) {
int n;
int* avrs;
int** lines;
FILE* f;
f
= fopen("test.dat", "r"); init(f, &avrs, &lines, n);
sort(avrs, lines, n);
print(avrs, lines, n);
term(avrs, lines, n);
return EXIT_SUCCESS;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnZvaWQgaW5pdChGSUxFKiBmLCBpbnQqKiBhdnJzLCBpbnQqKiogbGluZXMsIGludCBuKSB7CiAgIGNvbnN0IGludCBtID0gNTsKICAgaW50IGk7CiAgIGludCBqOwogICBpbnQgczsKICAgaW50KiBhczsKICAgaW50KiogbHM7CiAgIGludCogbDsKCiAgIC8qIHJlYWQgZGF0YSAqLwogICBscyA9IG1hbGxvYyhzaXplb2YoaW50KikgKiBuKTsKICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICBsID0gbWFsbG9jKHNpemVvZihpbnQpICogbSk7CiAgICAgIGZzY2FuZihmLCAiJWTjgIAlZOOAgCVk44CAJWTjgIAlZCUqYyIsICZsWzBdLCAmbFsxXSwgJmxbMl0sICZsWzNdLCAmbFs0XSk7CiAgICAgIGxzW2ldID0gbDsKICAgfQoKICAgLyogY29tcHV0ZSBhdmVyYWdlICovCiAgIGFzID0gbWFsbG9jKHNpemVvZihpbnQpICogbik7CiAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgbCA9IGxzW2ldOwogICAgICBzID0gMDsKICAgICAgZm9yIChqID0gMTsgaiA8IG07IGorKykgewogICAgICAgICBzICs9IGxbal07CiAgICAgIH0KICAgICAgYXNbaV0gPSBzIC8gKG0gLSAxKTsKICAgfQoKICAgKmF2cnMgPSBhczsKICAgKmxpbmVzID0gbHM7Cn0KCnZvaWQgc29ydChpbnQqIGF2cnMsIGludCoqIGxpbmVzLCBpbnQgbikgewogICBpbnQgaTsKICAgaW50IGo7CiAgIGludCB0MTsKICAgaW50KiB0MjsKCiAgIGZvciAoaSA9IDA7IGkgPCBuIC0gMTsgaSsrKSB7CiAgICAgIGZvciAoaiA9IDA7IGogPCBuIC0gMTsgaisrKSB7CiAgICAgICAgIGlmIChhdnJzW2pdIDwgYXZyc1tqICsgMV0pIHsKICAgICAgICAgICAgdDEgPSBhdnJzW2pdOwogICAgICAgICAgICBhdnJzW2pdID0gYXZyc1tqICsgMV07CiAgICAgICAgICAgIGF2cnNbaiArIDFdID0gdDE7CiAgICAgICAgICAgIAogICAgICAgICAgICB0MiA9IGxpbmVzW2pdOwogICAgICAgICAgICBsaW5lc1tqXSA9IGxpbmVzW2ogKyAxXTsKICAgICAgICAgICAgbGluZXNbaiArIDFdID0gdDI7CiAgICAgICAgIH0KICAgICAgfQogICB9Cn0KCnZvaWQgcHJpbnQoaW50KiBhdnJzLCBpbnQqKiBsaW5lcywgaW50IG4pIHsKICAgaW50IGk7CiAgIGludCogbGluZTsKICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICBsaW5lID0gbGluZXNbaV07CiAgICAgIHByaW50ZigiYXZyOiVk44CAJWTjgIAlZOOAgCVk44CAJWTjgIAlZFxuIiwgYXZyc1tpXSwgbGluZVswXSwgbGluZVsxXSwgbGluZVsyXSwgbGluZVszXSwgbGluZVs0XSk7CiAgIH0KfQoKdm9pZCB0ZXJtKGludCogYXZycywgaW50KiogbGluZXMsIGludCBuKSB7CiAgIGludCBpOwogICBmcmVlKGF2cnMpOwogICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgIGZyZWUobGluZXNbaV0pOwogICB9CiAgIGZyZWUobGluZXMpOwp9CgppbnQgbWFpbih2b2lkKSB7CiAgIGludCBuOwogICBpbnQqIGF2cnM7CiAgIGludCoqIGxpbmVzOwogICBGSUxFKiBmOwoKICAgZiA9IGZvcGVuKCJ0ZXN0LmRhdCIsICJyIik7CiAgIGZzY2FuZihmLCAiJWQlKmMiLCAmbik7CiAgIGluaXQoZiwgJmF2cnMsICZsaW5lcywgbik7CiAgIGZjbG9zZShmKTsKICAgc29ydChhdnJzLCBsaW5lcywgbik7CiAgIHByaW50KGF2cnMsIGxpbmVzLCBuKTsKICAgdGVybShhdnJzLCBsaW5lcywgbik7CiAgIAogICByZXR1cm4gRVhJVF9TVUNDRVNTOwp9