home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates the use of pointer with
- one-dimension arrays. Program calculates the average
- value of the data found in the array.
- */
-
- #include <iostream.h>
-
- const int MAX = 30;
-
- main()
- {
-
- double x[MAX];
- // declare pointer and initialize with base
- // address of array x
- double *realPtr = x; // same as = &x[0]
- double sum, sumx = 0.0, mean;
- int n;
- // obtain the number of data points
- do {
- cout << "Enter number of data points [2 to "
- << MAX << "] : ";
- cin >> n;
- cout << "\n";
- } while (n < 2 || n > MAX);
-
- // prompt for the data
- for (int i = 0; i < n; i++) {
- cout << "X[" << i << "] : ";
- // use the form *(x+i) to store data in x[i]
- cin >> *(x + i);
- }
-
- sum = n;
- for (i = 0; i < n; i++)
- // use the form *(realPtr + i) to access x[i]
- sumx += *(realPtr + i);
- mean = sumx / sum;
- cout << "\nMean = " << mean << "\n\n";
- return 0;
- }