Results 1 to 3 of 3
  1. #1
    rochla16 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Question standard deviation error

    My standard deviation is off, and I'm not sure why. I found this formula online, but there must be something wrong...Anyone have ideas? :confused:

    Java Code:
    double deviation=0;
    for (int i = 0; i < numbers.length; i++) {
    sum += Math.pow((numbers[i] - average), 2);
    }
    
    deviation = Math.sqrt(sum /(number_of_elements-1));
    System.out.println ("The SD is : " + deviation);

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Check that you are initialising sum to zero and that number_of_elements has the correct value. Also check that you are supposed to subtract one from it. (there are different ways of defining it). And check that average has the value you expect.

    If you still get a strange result post a Short, Self Contained, Correct Example: something that others can run as well as a description of the result you expected.

  3. #3
    rochla16 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Hmmm, I've checked all of that. Here's my code:

    Java Code:
    public class sample {
    public static void main (String[]args){
    java.util.Scanner input = new java.util.Scanner(System.in);
    
    final int number_of_elements=10;
    double [] numbers = new double [number_of_elements];
    double sum=0;
    
    
    for (int i=0; i < number_of_elements; i++) {
    System.out.print ("Enter a number: ");
    numbers[i] = input.nextDouble();
    sum+=numbers[i];
    
    }
    
    double average = sum/number_of_elements;
    System.out.println ("The mean is " + average); 
    
    
    
    double deviation=0;
    for (int i = 0; i < numbers.length; i++) {
    sum += Math.pow((numbers[i] - average), 2);
    }
    
    deviation = Math.sqrt(sum /(number_of_elements-1));
    System.out.println ("The SD is : " + deviation); 
     
    }
    }

Similar Threads

  1. standard deviation calculation
    By rochla16 in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 08:27 PM
  2. JSP standard actions
    By subhashis_mohanty in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-27-2010, 08:35 AM
  3. what is "standard error stream"
    By Cylab in forum New To Java
    Replies: 11
    Last Post: 07-15-2010, 09:29 AM
  4. [SOLVED] Calculating Std. Deviation using Java
    By random0munky in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 10:24 AM
  5. standard deviation and mean for an array
    By peterdfl in forum New To Java
    Replies: 3
    Last Post: 05-29-2008, 05:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •