Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Can anyone explain this code to me? Calculation of mean and variance.

1. ## Can anyone explain this code to me? Calculation of mean and variance.

```public class MeanVariance {
public static void main (String[] args) {
int len       = args.length;
double[] data = new double[len];
double mean, sum = 0, sum2 = 0, var;

for (int i = 0; i < len; i++) {
data[i] = Double.parseDouble(args[i]);
}

for (int i = 0; i < len; i++) {
sum += data[i];
}
mean = sum / len;
System.out.println(mean);

for (int i = 0; i < len; i++) {
sum2 += Math.pow((data[i] - mean),2);
}
var = sum2 / len;
System.out.println(var);
}
}```

I really don't understand this code which is used to calculate the mean and variance from a data set.

2. ## Re: Can anyone explain this code to me? Calculation of mean and variance.

Have you looked up the algorithms with google so you know what the code is supposed to do?

Which statements in the code are you having problems with?

3. ## Re: Can anyone explain this code to me? Calculation of mean and variance.

Yes, i've looked up into that and the code is supposed to calculate the mean and variance and print it out on two separate lines. The mean of N data items is calculated by adding up all the items and dividing the total by N. Variance is defined as the average of the squared distance between each item and the mean. I have problems understanding arrays so it would really help if you explain the code from scratch.

4. ## Re: Can anyone explain this code to me? Calculation of mean and variance.

I have problems understanding arrays so it would really help if you explain the code from scratch.
Start with the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

If there are any specific statements you don't understand, post the code and ask your questions about them.

5. ## The Following User Says Thank You to Norm For This Useful Post:

hemla (March 24th, 2013)

6. ## Re: Can anyone explain this code to me? Calculation of mean and variance.

`double mean, sum = 0, sum2 = 0, var;`

What does the above line mean?

7. ## Re: Can anyone explain this code to me? Calculation of mean and variance.

It defines 4 variables of type double and assigns values to two of them.

8. ## The Following User Says Thank You to Norm For This Useful Post:

hemla (March 24th, 2013)