Results 1 to 6 of 6
Thread: standard deviation calculation
 04282011, 01:26 AM #1Member
 Join Date
 Mar 2011
 Posts
 16
 Rep Power
 0
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; 04282011 at 02:50 AM.
 04282011, 02:16 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,669
 Rep Power
 13
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.
 04282011, 02:49 AM #3Member
 Join Date
 Mar 2011
 Posts
 16
 Rep Power
 0
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[middle1] + filearray[middle]) / 2; } } }
 04282011, 05:13 AM #4Member
 Join Date
 Mar 2011
 Posts
 50
 Rep Power
 0
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
AlLast edited by sibernewf; 04282011 at 05:15 AM.
 04282011, 06:09 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,669
 Rep Power
 13
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.
 04282011, 07:27 PM #6Member
 Join Date
 Mar 2011
 Posts
 16
 Rep Power
 0
Similar Threads

Jtextfield value get and calculation
By globo in forum New To JavaReplies: 11Last Post: 10252010, 04:51 AM 
Calculation with char
By chuckbalzer in forum New To JavaReplies: 7Last Post: 09202010, 05:29 PM 
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02252009, 12:44 AM 
[SOLVED] Calculating Std. Deviation using Java
By random0munky in forum New To JavaReplies: 1Last Post: 12122008, 10:24 AM 
standard deviation and mean for an array
By peterdfl in forum New To JavaReplies: 3Last Post: 05292008, 04:09 AM
Bookmarks