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