home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates the use of one-dimension
- arrays. The average value of the array is calculated.
- */
-
- #include <iostream.h>
-
- const int MAX = 30;
-
- main()
- {
-
- double x[MAX];
- double sum, sumx = 0.0, mean;
- int n;
-
- do { // obtain number of data points
- cout << "Enter number of data points [2 to "
- << MAX << "] : ";
- cin >> n;
- cout << "\n";
- } while (n < 2 || n > MAX);
-
- // prompt user for data
- for (int i = 0; i < n; i++) {
- cout << "X[" << i << "] : ";
- cin >> x[i];
- }
-
- // initialize summations
- sum = n;
-
- // calculate sum of observations
- for (i = 0; i < n; i++)
- sumx += x[i];
-
- mean = sumx / sum; // calculate the mean value
- cout << "\nMean = " << mean << "\n\n";
- return 0;
- }
-