Write a program that uses the array of numbers from problem 1. Then compute the
ID: 3926162 • Letter: W
Question
Write a program that uses the array of numbers from problem 1. Then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don't try to do the entire program at once. Build a little, test a little. Your program should contain 5 functions(methods). One method to compute the sum, a method to compute the average (mean), a method to compute the variance, a method to compute the standard deviation, and finally a method to compute the median. The method to compute the median should work regardless of whether n is even or odd, where n is the length of the array. Example for an odd sized array: {5, 7, 8, 9, 2} sum = 31.0 average (mean) = 6.2 Variance = [(5 - 6.2)^2 + (7 - 6.2)^2 + (8 - 6.2)^2 + (9 - 6.2)^2 + (2 - 6.2)^2]/5.0 = [(1.44) + (0.64) + (3.24) + (7.84) + (17.64)]/5.0 = 30.8/5.0 = 6.16 Standard Deviation = the squareroot of 6.16 = 2.482 The median for {2, 5, 7, 8, 9} is 7Explanation / Answer
Solution:
#include <iostream.h>
#include<math.h>
double sum (double sum_arr[], int sum_arr_sz)
{
int i;
double total;
// summing up the data
for (i=0; i<sum_arr_size; i++)
total = total + sum_arr[i];
return total;
}
double average( double avg_arr[], avg_arr_sz)
{
int i;
double total, averg;
// summing up the data
for (i=0; i<avg_arr_size; i++)
total = total + avg_arr[i];
//calculating average
averg= total / (double)avg_arr_size
return averg;
}
double variance(double var_arr[],double avg, int var_arr_sz)
{
int i;
double total,variance;
for (i = 0; i < var_arr_sz; i++)
{
return variance;
}
double std_deviation( double variance)
{
int i;
double std_devn;
std_devn = sqrt (variance);
return std_devn;
}
double median(med_arr[], int med_arr_sz)
{
double* sort_arr = new double[med_arr_sz];
for (int i = 0; i < med_arr_sz; ++i)
{ sort_arr[i] = med_arr[i];
}
for (int i = med_arr_sz - 1; i > 0; --i)
{
for (int j = 0; j < i; ++j)
{
if (sort_arr[j] > sort_arr[j+1])
{
double sort_temp = sort_arr[j];
sort_arr[j] = sort_arr[j+1];
sort_arr[j+1] = sort_temp;
}
}
}
// average of middle values in the sorted array
double Med = 0.0;
if ((med_arr_sz % 2) == 0)
{
Med = (sort_arr[med_arr_sz/2] + sort_arr[(med_arr_sz/2) - 1])/2.0;
}
else
{
Med = sort_arr[med_arr_sz/2];
}
delete [] sort_arr;
return Med;
}
int main()
{
double Arr_Values[] = {33.5, 67.5, 67.5, 88.0, 46.0, 94.5, 77.5, 83.0, 95.0, 80.5};
double sum_ret, avg_ret, var_ret;
int Array_Size = 10;
sum_ret = Sum(Arr_Values, Array_Size)
std::cout << "Sum = " << sum_ret<< std::endl;
avg_ret = average(Arr_Values, sum_ret, Array_Size)
std::cout << "Average(mean) = " << avg_ret << std::endl;
var_ret= variance(Arr_Values, avg_ret, Array_Size)
std::cout << "Variance = " << var_ret << std::endl;
std::cout << "Standard Deviation= the square root of"<<var_ret << "=" << std_deviation(var_ret)<< std::endl;
std::cout << "Median = " << median(Arr_Values, Array_Size) << std::endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.