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