array exercise

Basic Statistics Again -- with Array and Vector

Overview

Given a list of N floating point numbers, find the mean, maximum, minimum,  root-mean-square and standard deviation. Your program will first ask for N, the number of integers in the list, which the user will input. Then the user will input N more numbers.  You need to use array and vector to store the numbers and use functions for each calculation.

<< the vector implementation is in another page >>

--------------

#define    MAX_SIZE    30

float samples[MAX_SIZE];

int main()

{

    double min, max, mean;

    // input samples

    MinMaxMean(samples, n, &min, &max, &mean);

    cout << "min= " << min << '\t' << "max= " << max << '\t'

        << "mean= " << mean << endl;

min = CalculateMin(samples, n);

max = CalculateMax(samples, n);

mean = CalculateMean(samples, n);

    // another function to calculate root-mean-square

    // may be another one for standard deviation

}

In order to pass an array into a function, such as MinMaxMean(samples, n, &min, &max, &mean); above, we need to pass array content AND array size.  Note that min, max and mean are values returned from the routine.  We need to use "call by reference" or "call by pointer" in order to change their content.

Alternatively, one can use three functions as follows:

min = CalculateMin(samples, n);

max = CalculateMax(samples, n);

mean = CalculateMean(samples, n);

The CalculateMean function may be implemented as:

double CalculateMean(float S[ ], int size) {

double sum=0;

for (int i=0; i<size; i++) sum += S[i];

return sum/size;

}

If we use vector instead of array, then there is no need to pass the size of the vector.  It is part of the interface that has been implemented with vector STL.   My vector implementation is here.