This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# standard deviation of any sized array java

Kyle Wettengel
Greenhorn
Posts: 14
Hey guys I'm having trouble figuring out how to calculate the standard divination of an array in Java. As you can see I have already calculated the mean, and I know that at the end I will have to divide by the sample size minus 1 (n-1) and square that number. The problem I'm having is how to take every number and calculate how far it is away from the mean, then square that number. I know I could do every number from the data set separately but there has to be an easier way. Any help would be appreciated, here's my code.

Paul Clapham
Sheriff
Posts: 20764
30
Is your problem that you don't know what is meant by "how far it is away from the mean"? You'd take the number and subtract the mean, that's how. (Calling the variable which stores the mean value "average" wasn't a good choice, by the way.)

And yes, you do have to do that for each data point in the sample. So putting it inside the loop like you did, you're on the right track.

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
There is probably a package you can download that would have everything you need in it, but I'm guessing you are supposed to do this yourself.

The first steps when writing a program should always be to think through how you'd do it yourself, with pencil and paper. In order to figure out how far a number is from the mean, you need two things: the number, and the mean.

And you can't get the mean until you have looked at every number.

So yes, you do need to loop through your array (at least) twice. Once to get the mean, and once to get the difference between each number and the mean. There is no way to do it in one pass.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?