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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Unhappy 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)

    and ask google.

    If there are any specific statements you don't understand, post the code and ask your questions about them.
    If you don't understand my answer, don't ignore it, ask a question.

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

    hemla (March 24th, 2013)

  6. #5
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    hemla (March 24th, 2013)

Similar Threads

  1. Can someone please explain this code for me?
    By Grot in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 17th, 2013, 11:56 AM
  2. Could you explain this code to me please?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: December 7th, 2011, 02:10 PM
  3. Help with variance and standard deviation
    By alexpayne5657 in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 30th, 2011, 01:29 PM
  4. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  5. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM