//--------------------------------------------------------- // Filename: FindAvg2.cpp // // Purpose: Find the average of a list of values. The // values are entered by the user. // // D. Searls // Asbury College // Sep 2006 //--------------------------------------------------------- #include <iostream> #include <iomanip> using namespace std; const int MAXSIZE = 100; int main() { int numValues; // Number of values int count; // Number of values processed so far double x[MAXSIZE]; // A list of values double sum; // Sum of values double avg; // The arithmetic average // Get number of scores do { cout << "Enter the number of values in the list (up to " << MAXSIZE << "): "; cin >> numValues; cout << endl; } while (numValues > MAXSIZE); // Read scores for (count = 0; count < numValues; count++) { cout << "Enter score " << count + 1 << ": "; cin >> x[count]; } cout << endl; // Calculate average if (numValues > 0) { sum = 0.0; for (count = 0; count < numValues; count++) { sum = sum + x[count]; } avg = sum/(double)numValues; } // Display results if (numValues > 0) { cout << "Number of scores read: " << numValues << endl; cout << "Sum of scores: " << sum << endl; cout << fixed << setprecision(2) ; cout << "Average score: " << avg; } else { cout << "The list contained no scores."; } cout << endl << endl; return 0; } //--------------------------------------------------------- // Filename: FindAvg3.cpp // // Purpose: Find the average of a list of values. The // values are entered by the user. // // D. Searls // Asbury University // Oct 2011 //--------------------------------------------------------- #include <iostream> #include <iomanip> using namespace std; const int MAXSIZE = 100; //----------------------------------------------------- // getData // // Read a list of values from the keyboard into an array. // Return the values and the number of values. // // Out Parameters: num, arr //----------------------------------------------------- void getData(int& num, double arr[]) { num = 0; // Get number of scores do { cout << "Enter the number of values in the list (up to " << MAXSIZE << "): "; cin >> num; cout << endl; } while (num > MAXSIZE); // Read scores for (int count = 0; count < num; count++) { cout << "Enter score " << count + 1 << ": "; cin >> arr[count]; } cout << endl; } //----------------------------------------------------- // sum // // Given an array of values and the number of values // in the array, return the sum of the values. If the // number of values is less than or equal to zero, this // function will return a value of zero. // // In Parameters: size, list //----------------------------------------------------- double sum(int size, const double list[]) { double sum; // Calculate average sum = 0.0; for (int count = 0; count < size; count++) { sum = sum + list[count]; } return sum; } //----------------------------------------------------- // mean // // Given an array of values and the number of values // in the array, return the mean value. If the number // of values is less than or equal to zero, this // function will return a value of zero. // // In Parameters: n, data //----------------------------------------------------- double mean(int n, const double data[]) { return sum(n, data) / (double)n; } //----------------------------------------------------- // displayReport // // Generate a statistical report including the number // of values in the data set, the sum of the values, and // and the mean value. // // Precondition: num > 0 // // In Parameters: num, sum, average //----------------------------------------------------- void displayReport(int num, double sum, double average) { cout << "Number of scores read: " << num << endl; cout << "Sum of scores: " << sum << endl; cout << fixed << setprecision(2) ; cout << "Average score: " << average; } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { int numValues; // number of data values double myData[MAXSIZE]; // data values getData(numValues, myData); if (numValues > 0) { displayReport(numValues, sum(numValues, myData), mean(numValues, myData)); } else { cout << "The list contained no scores."; } cout << endl << endl; return 0; }