home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / array1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-30  |  846b  |  41 lines

  1. /*
  2.   C++ program that demonstrates the use of one-dimension
  3.   arrays.  The average value of the array is calculated.
  4. */
  5.  
  6. #include <iostream.h>
  7.  
  8. const int MAX = 30;
  9.  
  10. main()
  11. {
  12.  
  13.     double x[MAX];
  14.     double sum, sumx = 0.0, mean;
  15.     int n;
  16.  
  17.     do { // obtain number of data points
  18.         cout << "Enter number of data points [2 to "
  19.              << MAX << "] : ";
  20.         cin >> n;
  21.         cout << "\n";
  22.     } while (n < 2 || n > MAX);
  23.  
  24.     // prompt user for data
  25.     for (int i = 0; i < n; i++) {
  26.         cout << "X[" << i << "] : ";
  27.         cin >> x[i];
  28.     }
  29.  
  30.     // initialize summations
  31.     sum = n;
  32.  
  33.     // calculate sum of observations
  34.     for (i = 0; i < n; i++)
  35.         sumx += x[i];
  36.  
  37.     mean = sumx / sum; // calculate the mean value
  38.     cout << "\nMean = " << mean << "\n\n";
  39.     return 0;
  40. }
  41.