aboutsummaryrefslogtreecommitdiffstats
path: root/check.c
blob: 58e31ab3b6d41c8474157165c533bf49ee544160 (plain)
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
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	FILE ** files;
	FILE * output;
	size_t i;
	double t, sum, tmp1, tmp2;

	if (argc < 3) return 1;
	files = calloc(argc - 2, sizeof(FILE*));
	for (i = 1; i < argc - 1; ++i) {
		if (!(files[i-1] = fopen(argv[i], "r"))) {
			perror("opening files");
			return 1;
		}
	}

	output = fopen(argv[argc-1], "w+");
	for(;;) {
		t = 0;
		sum = 0;
		for (i = 0; i < argc - 2; ++i) {
			if (2 != fscanf(files[i], "%lf %lf", &tmp1, &tmp2)) goto end;
			t += tmp1;
			sum += tmp2;
		}
		t /= (argc-2);
		fprintf(output, "%f %f\n", t, sum);
	}
end:
	fclose(output);
	for (i = 0; i < argc - 2; ++i) fclose(files[i]);
	free(files);
	return 0;
}