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.
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)
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?
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:
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
\/
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.