File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes standard deviation of any sized array java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "standard deviation of any sized array java" Watch "standard deviation of any sized array java" New topic

standard deviation of any sized array java

Kyle Wettengel

Joined: Feb 06, 2013
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

Joined: Oct 14, 2005
Posts: 18987

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

Joined: Oct 02, 2003
Posts: 11498

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: standard deviation of any sized array java