home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates the pointers to manage
- dynamic data
- */
-
- #include <iostream.h>
-
- const int MAX = 30;
-
- main()
- {
-
- double* x;
- double sum, sumx = 0, mean;
- int *n;
-
- n = new int;
- if (n == NULL)
- return 1;
-
- do { // obtain number of data points
- cout << "Enter number of data points [2 to "
- << MAX << "] : ";
- cin >> *n;
- cout << "\n";
- } while (*n < 2 || *n > MAX);
- // create tailor-fit dynamic array
- x = new double[*n];
- if (!x) {
- delete n;
- return 1;
- }
- // 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";
- // deallocate dynamic memory
- delete n;
- delete [] x;
- return 0;
- }
-