home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / array3.cpp < prev    next >
C/C++ Source or Header  |  1993-03-26  |  781b  |  31 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. main()
  10. {
  11.  
  12.     double x[] = { 12.2, 45.4, 67.2, 12.2, 34.6, 87.4,
  13.                    83.6, 12.3, 14.8, 55.5 };
  14.     double sum, sumx = 0.0, mean;
  15.     int n;
  16.  
  17.     n = sizeof(x) / sizeof(x[0]);
  18.     sum = n;
  19.  
  20.     // calculate sum of observations
  21.     cout << "Array is:\n";    
  22.     for (int i = 0; i < n; i++) {
  23.         sumx += x[i];
  24.         cout << "x[" << i << "] = " << x[i] << "\n";
  25.     }
  26.     
  27.     mean = sumx / sum; // calculate the mean value
  28.     cout << "\nNumber of data points = " << n << "\n"
  29.          << "Mean = " << mean << "\n";
  30.     return 0;
  31. }