/* EECS 211 Sample Program - Average Author: Larry Henschen Contact: henschen@eecs.northwestern.edu Date: January 9, 2008 This program computes the average of a set of non-negative integers. It has many deficiencies and is meant only as a simple first illustration of a C++ program. */ #include using namespace std; main() { int number; // The number of scores read. int sum; // The summation of all the scores read. int grade; // Holds each individual score as it is // read from the keyboard. float average; // Holds the computed average. number = 0; // Initialize number and sum to 0 before starting. sum = 0; cin >> grade; // Get the first user input. // Loop as long as the input was non-negative. while(grade>=0) { sum = sum + grade; // Add new score into sum number = number + 1; // and count one more grade cin >> grade; // Now get the next input. } // When done reading scores, compute average and display. average = (float)sum/number; cout << average; // When done, exit the program. return 0; }