Problem description

We wrote a program to calculate mean, variance and standard deviation of a set of numbers in our previous assignment. You should be able to use the code you already wrote for this exercise. We like to read a set of numbers from a file whose name is hard-coded in the program as a const variable, and calculate the mean, variance, and the median of the numbers. In order for the medina to work, we must sort the data. Your program must read the data file just ONCE, because we're using arrays. Your program will NOT have any input form the user.

  • Create a uint type from the unsigned int data type using the typedef statement. Use this data type where you normally use the int data type (i.e. your loop variables).
  • The program will not ask for any input, but will read an arbitrary amount of numbers from a filename called numbers.txt . Display an error if this file can't be opened. Use a constant string located at the top of the program to specify the filename. You can assume the file will contain no more than 100 numbers.
  • Write four functions, mean (), variance (), median() , and sortMe() using the prototypes below. The variance () function will need to call the mean() function.
  • Implement a function, sortMe (), which takes an array of numbers and sorts them in ascending order. You can find an example implementation of a selection sort algorithm in the book or you can use any sorting algorithm.

Write the following functions as prototyped below. Do not alter the function signatures.

/// Computes the mean of an array containing n elements
/// @example mean(vals, 4) returns 3.5 if vals = { 2, 4, 3, 5}
double mean( const double values[], const uint n );

/// Computes the variance of an array containing n elements
/// @example variance(vals, 4) returns 1.666667
double variance( const double values[], const uint n );

/// Computes the median of an array containing n elements
/// @example median(vals, 4) returns 3.5
double median( const double values[], const uint n );

/// Sorts the elements of an array with n elements in ascending order
/// @example sortMe (vals, 4) changes the contents of vals
/// from {2, 4, 3, 5} to {2, 3, 4, 5}
void sortMe(double values[], const uint n );

Calculation of median of a set of numbers is pretty simple. For a sorted array of n elements, we calculate the median as follows:

If n is odd, the median is the middle number. The array element at n/2
If n is even, the median is the average of the 2 middle numbers. (array element at n/2-1 + array element at n/2) / 2

Median of 2, 3, 4, 5 would be: (3+4)/2 = 3.5
Median of 2, 3, 4, 5, 6 would be: 4

Write a client program to test your functions. You can create any data set to test your code, but your code must run with any data file. You can assume that there is no header line in the file and that the data can include any number (int or float).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.