/* program provides a framework for studying simple loop invariants the basic problem is to read a sequene of positive values and compute their average */ #include int main () { int count; double value; double sum; /* approach 1 */ printf ( "First Approach\n"); sum = 0.0; count = 0; printf ("Enter numbers (conclude with 0)\n"); scanf ("%lf", &value); while (value > 0) { sum = sum + value; count = count + 1; scanf ("%lf", &value); } printf ("average = %6.2lf\n", sum/count); /* approach 2 */ printf ("Second Approach\n"); sum = 0.0; count = 0; printf ("Enter numbers (conclude with 0)\n"); scanf ("%lf", &value); while (value > 0) { scanf ("%lf", &value); sum = sum + value; count = count + 1; } printf ("average = %6.2lf\n", sum/count); /* approach 3 */ printf ("Third Approach\n"); sum = 0.0; count = 0; value = 3.141592; printf ("Enter numbers (conclude with 0)\n"); while (value > 0) { scanf ("%lf", &value); if (value > 0) { sum = sum + value; count = count + 1; } } printf ("average = %6.2lf\n", sum/count); /* approach 4 */ printf ("Fourth Approach\n"); sum = 0.0; count = 0; printf ("Enter numbers (conclude with 0)\n"); do { scanf ("%lf", &value); if (value > 0) { sum = sum + value; count = count + 1; } } while (value > 0); printf ("average = %6.2lf\n", sum/count); /* approach 5 */ printf ("Fifth Approach\n"); sum = 0.0; count = 0; printf ("Enter numbers (conclude with 0)\n"); do { scanf ("%lf", &value); sum = sum + value; count = count + 1; } while (value > 0); printf ("average = %6.2lf\n", sum/count); } // end main