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

    Default standard deviation calculation

    I'm trying to get the user to enter 10 numbers and then get the average and standard deviation. I also need the mode and median, but that's for later! I had the average and entering 10 numbers working, but I can't get the standard deviation to work. I keep getting an expected ";" error. :/ Help please! Thanks in advance! :)

    Java Code:
    public class Numbers {
    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);
    
    
    }
    public static double deviation(double[] x, int i) {
    num += Math.pow(x[i] - average), 2.0;
    double den = n - 1;
    double deviation = Math.sqrt((num/den));
    return deviation;
    }
    System.out.println ("The standard deviation is " + devation);
    
    }
    }
    Last edited by rochla16; 04-28-2011 at 03:50 AM.

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

    Default

    When you post code it's a good idea to use the "code" tags: you put [code] and [/code] at the end. That way the indentation is preserved and it is easier to read. I think you can edit your post to fix it.

    At the moment the curly braces don't match and that can't be good.

    Also the line

    Java Code:
    num += Math.pow(x[i] - average), 2.0;

    doesn't make sense. Check where you are putting the closing parenthesis.

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

    Default

    okay, i put the [code] thing in. thanks for the tip. and i skipped over the standard deviation, but i'm trying to get the mode and median to print and it won't. :/ not sure how to fix this? any ideas? it compiles, but only the mean works.


    Java Code:
    public class Numbers2 {
    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);
    }
    
    
    public static double calculateMax(double[] filearray){
            double maximum = filearray[0];
            for (int i = 1; i < filearray.length; i++){
                if (filearray[i] > maximum){
                    maximum = filearray[i];
                    }
                }
                return maximum;
                }
    
        public static double calculateMin(double[] filearray){
            double minimum = filearray[0];
                for (int i = 1; i < filearray.length; i++){
                    if (filearray[i] < minimum){
                        minimum = filearray[i];
                        }
                    }
                    return minimum;
                    }       
    
    
    public static double calculateMode(double[] filearray){
    
            double maxValue = -1;
            double modeOfArray = calculateMode(filearray);
            int maxCount = 0;
            System.out.println ("This is the mode " + modeOfArray);
            for (int i = 0; i < filearray.length; i++){
                int count = 0;
            
                for(int j = 0; j < filearray.length; j++){
                    if(filearray[j] == filearray[i]){
                        count++;
                        }
                    }
                if (count > maxCount){
                    maxValue = filearray[i];
                    maxCount = count;
                }
            }
        return maxValue;
        }
    
        public static double calculateMedian(double[] filearray){
        double medianOfArray = calculateMedian(filearray);
        int middle = (filearray.length+1)/2;
        System.out.println ("The median is " + medianOfArray);
        if (filearray.length/2 == 1) {
    
            return filearray[middle];
        } else {
           
           return (filearray[middle-1] + filearray[middle]) / 2;
        }
    }
    }

  4. #4
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Smile

    Not sure if this will help with the median part, but here goes.

    There are 2 ways to determine a median value.

    One:
    ----
    1. Have an array of numbers and sort them from lowest value to highest value.
    2. Determine the length of the array and pick the middle number (array element 5 AND 6 in your case of 10 numbers) this would be the median in a sorted array.

    Two:
    ----
    Find the halfway value of all the numbers.
    1. Have an array of numbers.
    2. Add all the numbers together and divide by 2.
    3. This will give you the middle (median) number of all the numbers).

    From what I can see you are calculating your median value on the length of the array, and not the array contents.

    Dunno if this helped.

    Cheers

    Al
    Last edited by sibernewf; 04-28-2011 at 06:15 AM.

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

    Default

    i'm trying to get the mode and median to print and it won't.

    It's as simple as

    Java Code:
    System.out.println("The mean is " + calculateMode(numbers));

    (at the end of the main() method). Note that your methods return the median mode etc. The values get printed whereever they are returned to.

    -----

    In your calculateMedian() method you have to sort the array, as suggested by subernewf. Otherwise the number that's in the middle of the array may not be the middle number.

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

    Default

    That doesn't work either... :/ The only thing printing is the mean

Similar Threads

  1. Jtextfield value get and calculation
    By globo in forum New To Java
    Replies: 11
    Last Post: 10-25-2010, 05:51 AM
  2. Calculation with char
    By chuckbalzer in forum New To Java
    Replies: 7
    Last Post: 09-20-2010, 06:29 PM
  3. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 12:44 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
  •