/*This program was created to calculate the mean and standard deviation of four user entered values*/
//Beginning of Code
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
//Declare the variables
float number1, number2, number3, number4;
//Enter a value for the first number
cout << "Please enter a value for the first number:";
cin >> number1;
//Enter a value for the second number
cout << "Please enter a value for the second number:";
cin >> number2;
//Enter a value for the third number
cout << "Please enter a value for the third number:";
cin >> number3;
//Enter a value for the fourth number
cout << "Please enter a value for the value number four:";
cin >> number4;
//The formula to calculate the mean of the four user entered integers
double average = (number1 + number2 + number3 + number4)/4.0;
//Output of the mean to the screen
cout << "The average value is \n" << (number1 + number2 + number3 + number4)/4.0 << "\n";
//The formula to calculate the numerator that will be used in the standard deviation equation
double numerator = pow(number1 - average, 2.0) + pow(number2 - average, 2.0) + pow(number3 - average, 2.0) + pow(number4 - average, 2.0);
//Output of the standard deviation to the screen using the numerator calculated above
cout << "The standard deviation is \n" <<sqrt((numerator)/3.0) << "\n";
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double a, b, c, d, sum, stdev, n;
cout << "Please enter your four values" << endl;
cin >> a>> b>> c>> d;
cout << "Please enter n (the number of values)" << endl;
cin >> n;
sum = a + b + c + d;
double avg = sum/n;
double numerator = pow(a - avg, 2.0) + pow(b - avg, 2.0) + pow(c - avg, 2.0) + pow(d - avg, 2.0);
cout << "The mean value is \n" << avg<< endl;
cout << "The standard deviation is \n" <<sqrt(numerator/3) << endl;
return 0;
}
Now my question arises in, when I tried to calculate the standard deviation straight out, WITHOUT the numerator, I kept getting an error. Why do I need to seperate it out into a numerator to calculate the std deviation?
My second question was why can I not include average in the first double used, where a , b, c, d etc are stored?
It's not for homework purposes, it is something that Ihave already done and submitted. I merely want to learn. Thanks!
You might have tried to initialize avg to sum/n... that won't work... Otherwise your programs are fine.... But what I don't get is, if you're calculating average of four numbers, why do you take input for n? Shouldn't it be 4, user independent?
Average of two numbers: (a+b)/2
Average of three numbers: (a+b+c)/3
Average of four numbers: (a+b+c+d)/4
The denominator is automatically decided by the number of values you're calculating average for... So why take an input for it? Just makes your program vulnerable to incorrect results...