/* runningavg.c - We read from a file a sequence of integers. As we read them 
	we print out their ordinal position, their value, the minimum value 
	seen up to now, the average, and the maximum value. 
*/

#include <stdio.h>
#include <stdint.h>

int main(int argc, char*argv[]) 
{
    if (argc != 2) {
	printf("usage: %s filename\n", argv[0]);
	return 0;
    }
    FILE *fin = fopen(argv[1], "r");
    if (fin == NULL) {
	printf("cannot open %s\n", argv[1]);
	return 1;
    }
    int min = INT32_MAX;	/* The smallest integer seen up to now */
    int max = INT32_MIN;  /* The largest integer seen up to now */
    int count = 0;	/* The ordinal position of the previous integer */
    double avg;
    int current;

    while (fscanf(fin, "%d", &current) > 0) {
	if (current < min)
	    min = current;
	if (current > max)
	    max = current;
	avg = (avg*count + current)/(++count);
	printf("%5d %10d %10d %10d %12.2f\n", count, current, min, max, avg);
    }
    fclose(fin);
    return 0;
}
