#include #include #include int main(int argc, char *argv[]) { if(argc != 1+2) { fprintf(stderr, "usage: %s linesperavg delim\n", argv[0]); return 1; } char *delim = argv[2]; int linesperavg = atoi(argv[1]); char **list; size_t i, len; char line[1337]; int iter = 0; double *values; fgets(line, 1337, stdin); line[strcspn(line, "\n")] = 0; while(1) { explode(line, delim, &list, &len); if(iter == 0) { values = malloc(sizeof(double) * len); } for(i = 0; i < len; ++i) { values[i] = ( values[i] * (iter% linesperavg) + atof(list[i]) ) / ((iter% linesperavg)+1); // fprintf(stderr, "d: ( %f * (%d%% %d) + %f ) / ((%d%% %d)+1)\n", values[i], iter, // linesperavg, atof(list[i]), iter, linesperavg); // fprintf(stderr, "x: %f\n", values[i]); } if(iter% linesperavg == linesperavg-1) { for(int j = 0; j < len; j++) { if(j == len-1) { printf("%f", values[j]); } else { printf("%f%s", values[j], delim); } } printf("\n"); } /* free list */ for(i = 0; i < len; ++i) { free(list[i]); } free(list); fgets(line, 1337, stdin); if(feof(stdin)) { break; } // fprintf(stderr, "d: %s", line); line[strcspn(line, "\n")] = 0; iter++; } free(values); return 0; }