In: Computer Science
Write a program that takes, as input, five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x₁, x₂, x₃, x₄, and x₅, then the mean is:
x = (x₁+ x₂+ x₃+ x₄+x₅)/5
and the standard deviation is:
s = √(((x₁-x)²+(x₂-x)²+(x₃-x)²+(x₄-x)²+(x₅-x)²)/5)
Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.
Format your output with setprecision(2) to ensure the proper number of decimals for testing!
Code:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
float mean(float x1,float x2,float x3,float x4,float x5)
{
/*In this function we calculate the mean*/
float x;
x=(x1+x2+x3+x4+x5)/5;
/*Here we calcuate the mean*/
return x;
/*here we return the mean*/
}
float deviation(float x1,float x2,float x3,float x4,float x5,float
x)
{
/*This function calculate the standard
deviation*/
float s;
s=sqrt(
(pow(x1-x,2)+pow(x2-x,2)+pow(x3-x,2)+pow(x4-x,2)+pow(x5-x,2))/5
);
/*Calculating standard deviation*/
return s;/*returning the standard deviation*/
}
int main()
{
float x1,x2,x3,x4,x5,x,s;
cout<<"Enter 5 numbers: "<<endl;
cin>>x1;
cin>>x2;
cin>>x3;
cin>>x4;
cin>>x5;/*Reading 5 numbers form the
user*/
x=mean(x1,x2,x3,x4,x5);/*Here we get mean from the
mean function*/
s=deviation(x1,x2,x3,x4,x5,x);/*Here we get standard
deviation from the deviation function*/
cout<<"Mean:
"<<setprecision(2)<<x<<endl;
cout<<"Standard Deviation:
"<<setprecision(2)<<s<<endl;
/*Here we print the result*/
}
output:
Indentation: