standard deviation calculation

• 04-28-2011, 02:26 AM
rochla16
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! :)

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); } }```
• 04-28-2011, 03:16 AM
pbrockway2
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

Code:

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

doesn't make sense. Check where you are putting the closing parenthesis.
• 04-28-2011, 03:49 AM
rochla16
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.

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;     } } }```
• 04-28-2011, 06:13 AM
sibernewf
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
• 04-28-2011, 07:09 AM
pbrockway2
Quote:

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

It's as simple as

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.
• 04-28-2011, 08:27 PM
rochla16
That doesn't work either... :/ The only thing printing is the mean