// IEA 1996 // Example program - section 23.4 // A test program for the functions meanarray // and addarray. #include #include // Function prototypes float meanarray(int,float[]); void addarray(int,const float[],const float[],float[]); void printarray(int,const float[]); void main() { const int MAX = 10; float arr1[MAX] = {1.0,2.1,3.4,4.1,5.2,2.4,5.1,6.3,2.5,1.9}, arr2[MAX] = {3.1,2.3,6.2,8.1,2.5,5.1,2.8,1.6,4.2,5.1}, arr3[MAX]; float average; int i; average = meanarray(MAX,arr1); cout << "The array with elements " << endl; printarray(MAX,arr1); cout << endl << "has average value of " << average << endl; addarray(MAX,arr1,arr2,arr3); cout << endl << "The sum of the arrays " << endl; printarray(MAX,arr1); cout << endl << "and" << endl; printarray(MAX,arr2); cout << endl << "is" << endl; printarray(MAX,arr3); } // Function definitions float meanarray(int n, // IN no of elements float A[]) // IN array parameter // This function returns the average value of // the first n elements in the array A which // is assumed to have >= n elements. { float sum = 0.0; // local variable to // accumulate sum int i; // local loop control for (i = 0; i < n; i++) sum += A[i]; return sum/n; } // end meanarray void addarray(int size, // IN size of arrays const float A[], // IN input array const float B[], // IN input array float C[]) // OUT result array // Takes two arrays of the same size as input // parameters and outputs an array whose elements // are the sum of the corresponding elements in // the two input arrays. { int i; // local control variable for (i = 0; i < size; i++) C[i] = A[i] + B[i]; } // End of addarray void printarray(int n,const float A[]) // Prints the first n elements of the array A // five to a row. { int i; cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(1); for (i=0; i