Standard Deviation formula assistance

I have to write a C++ program that computes the mean and the standard deviation of a set of four integer number. The mean is the sum of the four values divided by 4. Using the SD formula where n=4 and xi refers to each of the four values. I'm stuck on the standard deviation formula and I'm not even sure if this program is correct.


#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

float numberOne;
float numberTwo;
float numberThree;
float numberFour;
float mean;
float standardDeviation;

numberOne = 1;
numberTwo = 2;
numberThree = 3;
numberFour = 4;
mean = (numberOne + numberTwo + numberThree + numberFour) / 4;
standardDeviation = ;

cout << "Input your first number." << numberOne << endl;
cout << "Input your second number." << numberTwo << endl;
cout << "Input your third number." << numberThree << endl;
cout << "Input your fourth number." << numberFour << endl;


mean = (numberOne + numberTwo + numberThree + numberFour) / 4;

cout << "The mean is " << mean << "and the standard deviation is " << standardDeviation << "." << endl;

return 0;
}
cout is used for output. cin is used for input. That you aren't using cin means your program isn't
accepting any user input. That's the first problem.
Ok that makes sense. Where do I put that?
Alright so this is where i've gotten to now. Everything works EXCEPT my sd formula. It looks totally correct to me but it's not giving me the right answer. (should be 1.118 if using integers 1,2,3,4)


#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

float numberOne;
float numberTwo;
float numberThree;
float numberFour;
float mean;
float standardDeviation;
float someInt;

numberOne = 1;
numberTwo = 2;
numberThree = 3;
numberFour = 4;
someInt = 4;
mean = (numberOne + numberTwo + numberThree + numberFour) / 4;
standardDeviation = sqrt((pow(numberOne-mean,2)) + (pow(numberTwo-mean,2)) +
(pow(numberThree-mean,2)) + (pow(numberFour-mean,2))/ someInt);

cout << "Input your first number :" << endl;
cin >> numberOne;
cout << "Input your second number:" << endl;
cin >> numberTwo;
cout << "Input your third number :" << endl;
cin >> numberThree;
cout << "Input your fourth number:" << endl;
cin >> numberFour;

cout << "The mean is " << mean << " and the standard deviation is " << standardDeviation << "." << endl;

return 0;
}
You can't bake the cake before you mix the batter.

You are calculating the mean and standard deviation before you even ask
the user for the numbers over which to calculate the mean and standard
deviation.

(Although in your case, you set the numbers to be 1, 2, 3, and 4 respectively,
calculate the mean and standard deviation across those, and ONLY THEN ask
the user to enter the numbers, at which point you ignore what the user
entered anyway.)

Also, check your parentheses on the sqrt() line... they are wrong, and that
is what is throwing off the calculation.
That makes total sense what you're saying and I can honestly say I didn't even think about that. How do you change it so that whatever #'s are entered into the output work within the program? I am not sure how...

Also, in regards to the parentheses, I tried every combination I could think of and I still got the same result. Help?
1
2
3
mean = (numberOne + numberTwo + numberThree + numberFour) / 4;
standardDeviation = sqrt((pow(numberOne-mean,2)) + (pow(numberTwo-mean,2)) +
(pow(numberThree-mean,2)) + (pow(numberFour-mean,2))/ someInt);


These lines calculate the mean and standardDeviation across the values currently contained in
the variables numberOne through numberFour. You want to read the user input into those
variables before doing the calculation. Your current code reads the input AFTER the calculation.

Look at the formula for standard deviation and then look at your formula. Hint: what are you
dividing by someInt? Look at your parentheses. It might help to remove the extraneous
parens and perhaps reformat:

1
2
3
4
5
6
standardDeviation = sqrt(
    pow(numberOne-mean,2) + 
    pow(numberTwo-mean,2) +
    pow(numberThree-mean,2) + 
    pow(numberFour-mean,2) / someInt
    );


Now you know that division is performed before addition, so I can add one extra set of parens
to your equation without changing its value.

1
2
3
4
5
6
standardDeviation = sqrt(
    pow(numberOne-mean,2) + 
    pow(numberTwo-mean,2) +
    pow(numberThree-mean,2) + 
    ( pow(numberFour-mean,2) / someInt )    // <--- added parens here
    );

Thanks again..you're really helping! The first part of what you said makes sense but I'm not sure how to change it so that it comes before the calculation....?

And for the parenthesis part; I changed it to see if your way worked and I still came up with the same number!

This is frustrating! I know i'm doing something wrong but I can't find out where!
Simply move the line of code that does the calculation to AFTER the code that reads the input.

Parens: I don't think you read my post. I was not spoonfeeding you the answer. I was just trying to
make it easier for you to see what formula you are implementing.

The formula for std dev is (for 4 data points):

1
2
3
4
5
6
      ______________________________________________________________________
     /
    /       ( x1 - mu )^2 + ( x2 - mu )^2 + ( x3 - mu )^2 + ( x4 - mu )^2
   /        _____________________________________________________________
  /                                                 4
\/


Your formula, as written, is:

1
2
3
4
5
6
      ______________________________________________________________________
     /
    /                                                       ( x4 - mu )^2
   /        ( x1 - mu )^2 + ( x2 - mu )^2 + ( x3 - mu )^2 + ___________
  /                                                              4
\/

Last edited on
Go to this page: http://cplusplus.com/doc/tutorial/operators/
and read the last section titled Precedence of Operators.

That should explain what order operations occur in a compound formula, and where and when you need to use () to get the order you want. Most of your parenthesis are extraneous and have no affect on the calculations, yet as jsmith is pointing out, you're missing the one critical set of parenthesis necessary to get the proper calculation.
Thanks to both of you. I figured it out now and it works great.

I apologize for not fully understanding the whole programming thing. I know that it can be frustrating to teach someone how to do something that, to you, is very easy. To some of us though, it is a challenege. Again, thanks for your help.
Topic archived. No new replies allowed.