CIS071 Lab09 - Simple Statistics with Arrays and I/O                      DUE: noon, Mar 31, 2006  


 

Objectives:  Arrays, subscripts; Array I/O, computing statistical means and medians.  THE INTERFACES FOR ALL FUNCTIONS ARE TO BE CAREFULLY DOCUMENTED.

 

Lab07: Simple Statistics and Arrays – with I/O:  Let x be a float array of SIZE 20.  Write a program that will read some number of floating point data (up to 20) into x and then compute the sum, the mean (average), and the median of these data.  Your program should print the initial data and then print the sum, mean and median of these data at the end of execution.

 

Solution

Stage 1 – Write a function getData which given an array x (as an INOUT argument) will read data into x and return this data to the main program.  The function will read data as long as user wants, but with the maximum of 20 entries.  It will return a value numValid, the number of entered numbers. Note that 0 < numValid <= SIZE. The function should ask user if he/she wants to enter another number, and if answer is ‘y’ the user should be prompted to enter a number; if the answer is ‘n’,  no new data are entered.

 

   int getData

      (double x[],   // INOUT: array to be filled

       int size);   // IN: size of the array to be filled

 

After your function getData has been written, write a main (test driver) program to test the function.  Your main program should call getData and print numValid and the data read once getData returns.

  

Stage 2 – Keep the main program and getData functions from Stage 1 and now write two more functions, computeSum and computeMean.

 

   // Compute the sum of n items of type double data in array x

   // Returns – The sum of the items

   double computeSum

      (int n,        // IN: number of items in array (n <= 20)

       double x[]);   // IN: array of data

       

   // Compute the mean of n items given the sum

   // Returns – The mean of the items

   double computeMean

      (int n,       // IN: the number of items in the array (n <= 20)

      (double sum);  // IN: the sum of the data in the array

 

Modify the main program to call these functions (in order, after calling getData) and then display the results of the execution of the functions.

 

Stage 3 – Write the function computeMedian:

 

This stage is complicated because it requires sorting the array. 

 

   // Returns – The median of the items

   // If n is odd the median is the middle item in the array.

   // If n is even the median is the average of the two middle

   //    items in the array.

   double computeMedian

      (int n,        // IN: number of items in array

       double x[]);   // IN: array of data sorted in ascending order

                                                                                                                                  

 To write computeMedian, you should write another function sortArray that sorts elements of array. Figure 8.17 from the textbook should be helpful to you.

 

   void sortArray

      (int n,        // IN: number of items in array

       double x[]);   // IN: array of data sorted in ascending order

 

Deliverables:
Submit your program; show a few sample runs of the program.