// FILE: ShowDiff.cpp // COMPUTES THE AVERAGE VALUE OF AN ARRAY OF DATA AND PRINTS // THE DIFFERENCE BETWEEN EACH VALUE AND THE AVERAGE #include #include #include "apvector.h" int main() { // Local data ... const int num_items = 8; apvector x(num_items); // array of data int i; // loop control variable double average; // average value of data double sum; // sum of the data // Enter the data. cout << "Enter " << x.length() << " numbers: "; for (i = 0; i < x.length(); i++) cin >> x[i]; // Compute the average value. sum = 0.0; // initialize sum for (i = 0; i < x.length(); i++) sum += x[i]; // add each element to sum average = sum / x.length(); // get average value // Set i/o flags and precision for output to ensure 1 decimal // place for output. cout.setf (ios::fixed); cout.precision (1); cout << "The average value is " << average << endl << endl; // Display the difference between each item and the average. cout << "Table of differences between x[i] and the average." << endl; cout << setw (4) << "i" << setw (8) << "x[i]" << setw (14) << "difference" << endl; for (i = 0; i < x.length(); i++) cout << setw (4) << i << setw (8) << x[i] << setw (14) << (x[i] - average) << endl; return 0; }