Assignment: #stats0 Due: 10/13/2017 Points: 4

Specification

This C++ program computes some statistics using integer arrays as data sets.

The code uses function overloading, reference parameters, and default arguments.

The following four functions must be defined (i.e. implemented/programmed/coded).

   // function prototypes...
   int stats(int[], int, double&, double&, int&);
   int stats(int[], int, bool = false);
   int stats(const char[], int, double, double, int);
   int stats(const char[], int, bool = false);

The main() function for this program calls the four stats() functions.

int stats(int set[], int nelems, double& mean, double& median, int& mode)

This function determines the mean, median, and mode for parameter set. Parameter nelems is the cardinality of set (i.e. the number of array elements). The mean, median, and mode values are returned to the caller via the mean, median and mode reference parameters. Note: In this version of the program the median and mode do not need to implemented (i.e. set them equal to zero).

This function returns the range of parameter set.

int stats(int set[], int nelems, bool product)

If product is true, then the function returns the product of the elements of set; else the function returns the sum of the elements of set.

int stats(const char heading[], int range, double mean, double median, int mode)

This function prints a line to the standard output stream that is formatted like the following.

   heading range=range; mean=mean; median=median; mode=mode

The return value of this function can be any integer.

int stats(const char heading[], int n, bool product)

This function prints to the standard output stream the value of parameter heading followed the value of parameter n prefixed by either "sum=" or "product=" conditional on the value of parameter product. Pseudo-code for this function.

   print heading
   if (product) print "product="
   else print "sum="
   print heading sum=N

The return value of this function can be any integer.

Program Data Sets

The main() function should use the following data sets. You are free to copy and paste the following code into your main() function.

   int seta[] = { 5, 10, 15, 20, 25 }; 
   int alen = sizeof(seta) / sizeof(seta[0]);

   int setb[] = { 2, -2, 0, 2, -2, 2, -2, -2 };
   int blen = sizeof(setb) / sizeof(setb[0]);

   int setc[] = { 10, 9, 8, 10, 1, -1 };
   int clen = sizeof(setc) / sizeof(setc[0]);

All four stats() functions must be called for each set. Here is code for calculating the stats for one data set. You are free to copy and paste the following statements into your main function.

   double mean, median; 
   int mode, range;

   range = stats(seta, alen, mean, median, mode);
   (void) stats("set A: ", range, mean, median, mode);
   (void) stats("set A: ", stats(seta, alen));
   (void) stats("set A: ", stats(seta, alen, true), true);

Note: This is not an interactive program. The "inputs" to this program are hard-coded data sets (defined as int[] arrays).

Program Output

The output of the program must match the following.

   set A: range=20; mean=15; median=0; mode=0
   set B: range=4; mean=-0.25; median=0; mode=0
   set C: range=11; mean=6.16667; median=0; mode=0
   set A: sum=75
   set A: product=375000
   set B: sum=-2
   set B: product=0
   set C: sum=37
   set C: product=-7200