/* * compute variance of doubles entered as input */ #include #define N 10 double square(double x) { return x*x; } double variance(void) { double a[N]; int count = 0; /* read numbers into array */ double temp; while ((scanf("%lf", &temp) == 1) && (count < N)) { a[count] = temp; count += 1; } /* FIXME nice way to print warning if we stopped because array is full? */ /* compute average */ double sum = 0.0; for (int i = 0; i < count; ++i) { sum += a[i]; } double avg = sum/count; /* FIXME uncomment for testing? printf("avg %f\n", avg); */ /* compute variance */ sum = 0.0; for (int i = 0; i < count; ++i) { /* FIXME uncomment for tesing? printf("a[i] %f square %f\n", a[i], square(a[i] - avg)); */ sum += square(a[i] - avg); } return sum/count; } int main(void) { printf("enter doubles, anything else to stop\n"); printf("variance %f\n", variance()); return 0; }