This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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