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 3 of 3

Thread: Need help with my standard deviation code.

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with my standard deviation code.

    Here is the data set that is given as an example:

    1 2 3 4.5 5.6 6 7 8 9 10

    They give the answer as: 2.99794

    Here is my code:
    import java.util.*;
    public class Exercise05_21 {
        public static void main(String [] args){
            Scanner s = new Scanner(System.in);
     
            System.out.print("Enter ten numbers: ");
     
                double[]x = new double[10];
     
                int i;
     
                for(i = 0;i < 10; i++){
     
                    x[i] = s.nextDouble();
                }
     
     
                double mean = mean(x, i);
     
                double deviation = var(x);
     
                System.out.println("The mean is " + mean);
     
                System.out.println("The standard deviation is " + deviation);
     
        }
     
    public static double sum(double[] a) {
            double sum = 0.0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i];
            }
            return sum;
        }
     
     
     
     
     
     
     
        public static double mean(double[]x, double i){
     
            if (x.length == 0) return Double.NaN;
            double sum = sum(x);
            return sum / x.length;
        }
     
     
     
     
     
     
        public static double var(double[] x) {
            if (x.length == 0) return Double.NaN;
            double avg = mean(x, 10);
            double sum = 0.0;
            for (int i = 0; i < x.length; i++) {
                sum += (x[i] - avg) * (x[i] - avg);
            }
            return sum / (x.length - 1);
        }
     
     
    }



    I am not sure if my code is just wrong, if I am using the incorrect formula for standard deviation, or both. I've been working on this all freakin day and my head is about to explode. I suck at this stuff. If anyone can help I'd really appreciate it.
    Last edited by helloworld922; April 16th, 2011 at 02:35 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with my standard deviation code.

    return sum / (x.length - 1);
    You forgot to take the square root.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    drew.field (April 16th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my standard deviation code.

    That did it! Thanks so much.

Similar Threads

  1. Replies: 7
    Last Post: November 23rd, 2010, 10:03 PM
  2. STANDARD CALCULATOR
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 27th, 2010, 08:41 PM
  3. How to capture standard output from another program ?
    By ni4ni in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2010, 10:00 AM