Write a program that uses the array of numbers from problem

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 the merlian, The method to compute the medianshoud work reardlss f whether in is Example for an odd sized armay: (5, 7, 8, 9, 2) sum = 31 . O average (mean) = 62 Variance = [ (5-62)\' + (7-6.29 + (8-6.2)\' + (9-6.2)\' + (2-6.29] / 5.0 = [( 1.44) + (0.64) + (3.24) + (7.84) + (1764)] / 5.0 30.8/5.0 =6.16 Standard Deviation = the square root of 6·16 = 2 . 482 The median for (2, 5, 7, 8, 9 is 7

Solution

Please follow the code and comments for description :

CODE :

#include <iostream> // required header files
#include <string>
#include<math.h>

using namespace std;

double sum(double a[], int n); // function declarations
double avgMean(double sum, int n);
double variance (double a[], double mean, int n);
double sd(double varVal);
double median(double a[], int n);


double sum(double a[],int n) //Calculates its sum using a function
{
   double sum = 0; // required initialsiations
   for(int i = 0; i < n; i++) // iterating over the loop
   {
       sum = sum + a[i];
   }
   return sum;
}

double avgMean(double sum, int n) { // function to calculate the mean of the array
   double avg = sum/n;
   return avg; // returning the value
}

double variance (double a[], double mean, int n) { // function to calculate the variance of the array
   double var = 0;
   for (int i = 0; i < n; i++) {
       var += (a[i] - mean) * (a[i] - mean); // code to calculate the variance
   }
   return var /= n; // returning the variance value after dividing with the size of the array
}

double sd(double varVal) { // function to calculate the standard deviation of the array
   double sdValue = sqrt(varVal);
   return sdValue; // return the value
}

double median(double a[], int n) { // function to calculate the median of the array
   int i,t,f;
   double even,odd;
   t = (n/2); // getting the temp values
   f = (n/2)-1;

   even = (((a[t]+a[f]))/2);
   odd = a[t];
  
   for(i = 0; i < n; i++)
   {
       if(n%2 == 0){ // checking for the array element size
           return even; // return the value accordingly
       } else {
           return odd;
       }
   }
}

int main() // driver method
{
   int size = 0; //Array size
   cout << \"Enter the Size of the Array : \"; // prompt to enter the data
   cin >> size; // getting the size
   double array [size]; //Declaring array
  
   for(int i = 0; i < size; i++) //Loop which inputs arrays data and
  
   {
       cout << \"Enter element number \" << i+1 << \" : \"; // prompt to enter the elements
       cin >> array[i]; // getting the elements
      
   }
   double sumRes = sum(array, size); // calling the function and storing the result
   cout << \"The Sum of the Elements Entered is : \" << sumRes << endl; // printing the result
  
   double mean = avgMean(sumRes, size); // calling the function and storing the result
   cout << \"The Mean (Average) of the Entered Elements is : \" << mean << endl; // printing the result
  
   double varianceVal = variance(array, mean, size); // calling the function and storing the result
   cout << \"The Variance of the Entered Elements is : \" << varianceVal << endl; // printing the result
  
   double sdVal = sd(varianceVal); // calling the function and storing the result
   cout << \"The Standard Deviation of the Entered Elements is : \" << sdVal << endl; // printing the result
  
   double medianVal = median(array, size); // calling the function and storing the result
   cout << \"The Median of the Entered Elements is : \" << medianVal << endl; // printing the result
  
   return 0;
}

OUTPUT :

CASE 1 :

Enter the Size of the Array : 5
Enter element number 1 : 2
Enter element number 2 : 5
Enter element number 3 : 7
Enter element number 4 : 8
Enter element number 5 : 9
The Sum of the Elements Entered is : 31
The Mean (Average) of the Entered Elements is : 6.2
The Variance of the Entered Elements is : 6.16
The Standard Deviation of the Entered Elements is : 2.48193
The Median of the Entered Elements is : 7


CASE 2 :

Enter the Size of the Array : 10
Enter element number 1 : 33.5
Enter element number 2 : 67.5
Enter element number 3 : 67.5
Enter element number 4 : 88.0
Enter element number 5 : 46.0
Enter element number 6 : 94.5
Enter element number 7 : 77.5
Enter element number 8 : 83.0
Enter element number 9 : 95.0
Enter element number 10 : 80.5
The Sum of the Elements Entered is : 733
The Mean (Average) of the Entered Elements is : 73.3
The Variance of the Entered Elements is : 369.66
The Standard Deviation of the Entered Elements is : 19.2265
The Median of the Entered Elements is : 70.25


Hope this is helpful.

 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 va
 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 va
 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 va

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site