I’m trying to find the standard deviation of an array using a for loop.
I have some code that might work but it gives me errors.
I would appreciate some guidance and help! 🙂
Here is the code:
double StandardDeviation() {
double Dog,Variance,StandardDeviationFormula;
for (int k = 0; k < TheArrayAssingment.length; k++) {
Dog = Dog + (TheArrayAssingment[k] - Average())
* (TheArrayAssingment[k] - Average());
Variance = Dog / (TheArrayAssingment.length - 1);
StandardDeviationFormula = Math.sqrt(Variance);
}
return StandardDeviationFormula;
}
Since you are obviously a beginner at programming in general as well as new to Java, here is some general advice:
Always following the coding standards / convention. They are there to make you code more readable for other people. Do it even in little throw-away examples, so that you get into the habit of doing it in the cases where it matters.
It is a “universal” convention in Java that method and variable names start with a lower-case letter. You have started them all with an upper-case letter. Change
StandardDeviationtostandardDeviation,Variancetovarianceand so on.Choose variable and method names that accurately reflect the intended meaning. For instance:
Dogis obviously meaningless.TheArrayAssingmentmight be meaningful, but I can’t figure it out.StandardDeviationFormulais inaccurate. It doesn’t contain a formula. It contains a value that is the result of applying a formula.It also helps if you spell method and variable names correctly …
(There are exceptions to this. For instance, most seasoned programmers think it is perfectly acceptable to use conventional and abbreviated names for local variables when the meaning is self-evident. For example,
exorefor an exception,i/j/kfor a loop variable,itfor an iterator object. As you read more of other peoples’ code, you will get to see / understand the “idioms”.)