Question: Using the posted statistics functions, create your own header and source files to form a library. Write a program in which you create an array
Using the posted statistics functions, create your own header and source files to form a library. Write a program in which you create an array of 5001 real numbers within the range 20.0 to 50.0. Using your library, calculate the following values for your data: maximum, minimum, mean or average, median, standard deviation, and variance. Create your own function to count how many values fall within a specified range. The prototype for this function should look something like the following:
int count(double x[ ], int n, double lower, double upper);
where x[ ] is an array, n is the size of the array, lower is the specified lower limit of the range, and upper is the specified upper limit of the range. The function returns a count of the number of values in array x that fall within the range between lower and upper, not including the limits themselves.
In your program, call this function multiple times to calculate the number of values in the array that fall within the ranges 20 to 30, 30 to 40, and 40 to 50. Output all results.
statistics functions
#include
using namespace std;
/*---------------------------------------------------------*/
/* This function returns the maximum */
/* value in the array x with n elements. */
double maxval(double x[], int n)
{
// Declare local objects.
double maxVal;
// Determine maximum value in the array.
maxVal = x[0];
for (int k=1; k { if (x[k] > maxVal) maxVal = x[k]; } // Return maximum value. return maxVal; } /*---------------------------------------------------------*/ /* This function returns the minimum */ /* value in the array x with n elements. */ double minval(double x[], int n) { // Declare local objects. double min_x; // Determine minimum value in the array. min_x = x[0]; for (int k=1; k<=n-1; ++k) { if (x[k] < min_x) min_x = x[k]; } // Return minimum value. return min_x; } /*---------------------------------------------------------*/ /* This function returns the average or */ /* mean value of an array with n elements. */ double mean(double x[], int n) { // Declare and initialize objects. double sum(0); // Determine mean value. for (int k=0; k { sum += x[k]; } // Return mean value. return sum/n; } /*---------------------------------------------------------*/ /* This function returns the median */ /* value in an array x with n elements. */ /* The values in x are assumed to be ordered. */ double median(double x[], int n) { // Declare objects. double median_x; int k; // Determine median value. k = floor(double(n)/2); if (n%2 != 0) median_x = x[k]; else median_x = (x[k-1] + x[k])/2; // Return median value. return median_x; } /*---------------------------------------------------------*/ /* This function returns the variance */ /* of an array with n elements. */ // Function prototype. double mean(double x[], int n); double variance(double x[], int n) // Function header. { // Declare objects. double sum(0), mu; // Determine variance. mu = mean(x,n); for (int k=0; k { sum += (x[k] - mu)*(x[k] - mu); } // Return variance. return sum/(n-1); } /*---------------------------------------------------------*/ /* This function returns the standard deviation */ /* of an array with n elements. */ // Function prototype. double variance(double x[], int n); double std_dev(double x[], int n) // Function header. { // Return standard deviation. return sqrt(variance(x,n)); } /*---------------------------------------------------------*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
