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

Solution

#include <iostream>
using namespace std;
#include<math.h>

double sumArray(int array[],int size) //sum of array elements
{
   int i;
   double sum=0;
   for(i=0;i<size;i++)
   sum=sum+array[i];
   return sum;
}
double averageArray(int array[],int size) //average of array elements
{
   int i;
   double sum=0;
   for(i=0;i<size;i++)
   sum=sum+array[i];
   return sum/size;
}
double varianceArray(int array[5],double average,int size) //variance
{
   int i;
   double variance=0;
   for(i=0;i<size;i++)
   variance=variance+(array[i]-average)*(array[i]-average);
   return variance/size;
}
double stdDevArray(int array[],double variance,int size) //standard deviation
{
   return sqrt(variance);
}
double medianArray(int array[],int n) //median
{
   int i,j,temp;
   double median;
      
      
   for(i=0;i<n;i++) //sorting
{
   for(j=0;j<n-1-i;j++)
   {
      if(array[j]>array[j+1])
      {
      temp=array[j];
      array[j]=array[j+1];
      array[j+1]=temp;
      }
   }
}
     
  
   if(n%2 ==0)
   median = (array[n/2] +array[n/2 +1])/2;
   else
   median = array[n/2];
   return median;
}

int main()
{
   int i,array[20],size;
   double sum,average,stddev,median,variance;
   cout<<\"\ Enter the size of Array\";
   cin>>size;
   cout<<\"\ Enter the elements\";
   for(i=0;i<size;i++)
   cin>>array[i];
  
   sum = sumArray(array,size);
   cout<<\"\ Sum =\"<<sum;
  
   average = averageArray(array,size);
   cout<<\"\ Average = \"<<average;
  
   variance = varianceArray(array,average,size);
   cout<<\"\ Variance = \"<<variance;
  
   stddev= stdDevArray(array,variance,size);
   cout<<\"\ Standard Deviation=\"<<stddev;
  
   median = medianArray(array,size);
   cout<<\"\ median =\"<<median;
  
   return 0;
}

Output:

Success time: 0 memory: 3472 signal:0

 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