/* avg2.c: find avg in counting sample output data */ #include #include #include int main(int argc, char ** argv) { int N; double p, x, cum; int c1, c2, count; N = 0; cum = 0; scanf("%lf\n", &p); if ( p >= 1 ) { printf("Warning: first value in input = %lf (a value < 1 was expected)\n", p); printf("You have probably used INCORRECT input data\n"); printf("This program computes the avg for the OUTPUT data files\n"); printf("Use avg to compute the avg for the INPUT file !\n"); exit(1); } while ( scanf("%lf%d\n", &x, &count) > 0 ) { c1 = (int) count - 1; c2 = (int) 1/p; cum += (c1 + c2) * x; // Cummulative N += (c1 + c2); // Count /* printf("%d count=%d\n", j, count[j]); */ } printf("N=%d cum=%lf\n", N, cum); printf("Avg = %lf\n", cum/N); }