In: Computer Science
In this lab, you will write a function that takes five (5) doubles as input and passes back the mean (average) and the standard deviation. Your function should be named mean_std and it should return the mean (a double) and pass the standard deviation (also a double) back by reference.
The mean and standard deviation are a common calculation in statistics to express the range of "typical" values in a data set. This brief article explains how to calculate the standard deviation: http://www.mathsisfun.com/data/standard-deviation.html (Just the first part, up to where it says "But ... there is a small change with Sample Data".
Use the pow and sqrt functions from the cmath library in your function.
Passing in five separate numbers in five variables is annoying, but we haven't talked yet about passing around sets of numbers. That'll come in the next module.
Pay attention! You are passing back the two calculated values not printing them to the screen! There is no user input/output in this problem! Use what is given.
#include <iostream>
// including cmath to get access to pow and sqrt
#include <cmath>
using namespace std;
// put your function here, and name it mean_std
// main will be called when you are in "Develop" mode, so that you
can put testing code here
// when you submit, unit tests will be run on your function, and
main will be ignored
int main() {
return 0;
}
Answer
#include <iostream>
#include <cmath>
using namespace std;
double mean_std(double num1,double num2,double num3,double
num4,double num5,double &stdDev)
{
double avg=(num1+num2+num3+num4+num5)/5;
double variance =
(pow((num1-avg),2)+pow((num2-avg),2)+pow((num3-avg),2)+pow((num4-avg),2)+pow((num5-avg),2))/5;
stdDev= sqrt(variance);
return avg;
}
int main()
{
double num1,num2,num3,num4,num5,mean,stdDeviation;
num1=1.1;
num2=2.2;
num3=3.3;
num4=4.4;
num5=5.5;
mean=mean_std(num1,num2,num3,num4,num5,stdDeviation);
//JUST TO SHOW OUTPUT
cout<<"\nNum 1 : "<<num1;
cout<<"\nNum 2 : "<<num2;
cout<<"\nNum 3 : "<<num3;
cout<<"\nNum 4 : "<<num4;
cout<<"\nNum 5 : "<<num5;
cout<<"\nMean of Numbers is : "<<mean;
cout<<"\nStandard Deviation of Numbers is :
"<<stdDeviation;
return 0;
}
OUTPUT