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

  1. /*
  2.   C++ program that demonstrates the use of single-dimensional
  3.   arrays.  The average value of the array is calculated.
  4.   The array has its values preassigned internally.
  5. */
  6.  
  7. #include <iostream.h>
  8.  
  9. const int MAX = 10;
  10.  
  11. main()
  12. {
  13.  
  14.     double x[MAX] = { 12.2, 45.4, 67.2, 12.2, 34.6, 87.4,
  15.                       83.6, 12.3, 14.8, 55.5 };
  16.     double sum = MAX, sumx = 0.0, mean;
  17.     int n = MAX;
  18.  
  19.     // calculate sum of observations                
  20.     cout << "Array is:\n";
  21.     for (int i = 0; i < n; i++) {
  22.         sumx += x[i];               
  23.         cout << "x[" << i << "] = " << x[i] << "\n";
  24.     }
  25.     
  26.     mean = sumx / sum; // calculate the mean value
  27.     cout << "\nMean = " << mean << "\n\n";
  28.     return 0;
  29. }
  30.