/* This program uses a running total and count to compute the average of a set of grades entered by the user. Each time a new grade is entered, its value is added to the current total, and 1 is added to the current count. At the end, the final total and count are used to compute the average. This program also uses a sentinel to allow the user to execute at any time. That sentinel is -1 (hopefully, not a legal grade). */ #include main() { int grade, /* current grade entered by user */ total, /* running total of grades entered so far */ count; /* running count of how many grades ented so far */ /* Initialize total and count to 0, which is the total and number of grades before any are entered. */ total = 0; count = 0; /* Initialize grade to first value entered by user */ cout << "Enter first grade (-1 to quit): "; cin >> grade; /* Continue looping as long as the user does not enter the sentinel -1 for grade */ while (grade != -1) { /* Increment the current total by the new grade */ total = total + grade; /* Increment the total number of grades */ count = count + 1; /* Prompt user for next grade */ cout << "Enter next grade (-1 to quit): "; cin >> grade; } /* End of loop */ /* Print out the final average, based on current total and count. Make sure that number of grades is more than zero first (otherwise, will get division by zero) */ if (count > 0) cout << "The average is " << (float)total/count << "\n"; else cout << "No grades were entered\n"; }