Write a program that uses the array of numbers from problem
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.


