Results 1 to 2 of 2
 12122008, 07:23 AM #1Member
 Join Date
 Oct 2008
 Location
 Washington, US
 Posts
 28
 Rep Power
 0
[SOLVED] Calculating Std. Deviation using Java
I'm sure this has been posted before, but I'm just having a little tough time with it. The sum and mean calculates correctly but for some reason the standard deviation doesn't calculate correctly, here's my code. Any help is great appreciated =D
Java Code:private static double stddev(double[] doubleArray) { int i ; double avg = 0 ; double sum = 0 ; double tempSum = 0 ; // sum of the array for(i = 0; i < doubleArray.length; i++) { sum = sum + doubleArray[i] ; System.out.println(sum) ; } // average of the array avg = sum / (doubleArray.length) ; System.out.println("Mean = " + avg) ; // the length of the array minus 1 tempSum = (doubleArray.length)  1 ; // (x  average of x's)^2 for(i = 0; i < doubleArray.length; i++) { sum = sum + (Math.abs(doubleArray[i]  avg) * Math.abs(doubleArray[i]  avg)) ; } // (x  average of x's)^2 divided by length of the array minus 1 sum = sum / tempSum ; if (i == 0) { return 0.0 ; } // sqrt of the above return Math.sqrt(sum) ; } public static void main(String[] args) { //*********************************************************** // Testing standard deviation System.out.println("Testing variance\n"); double[] testDoubleArray = { 2.0, 3, 5, 6.0, 0.0 }; // You should try these test cases too //double[] testDoubleArray = { 2.0, 2.0 }; //double[] testDoubleArray = {} System.out.println("Test array for standard deviation is: "); for (i=0; i < testDoubleArray.length; i++) { System.out.print(testDoubleArray[i] + " "); } System.out.println("\nStandard deviation is: " + stddev(testDoubleArray)); System.out.println("\n***************************");
 12122008, 10:24 AM #2Member
 Join Date
 Oct 2008
 Location
 Washington, US
 Posts
 28
 Rep Power
 0
Similar Threads

standard deviation and mean for an array
By peterdfl in forum New To JavaReplies: 3Last Post: 05292008, 04:09 AM 
Calculating per hour statistics
By vipergt89 in forum New To JavaReplies: 2Last Post: 05012008, 06:25 AM 
Calculating trigonometric functions
By Java Tip in forum java.langReplies: 0Last Post: 04162008, 10:56 PM 
Calculating hyperbolic functions
By Java Tip in forum java.langReplies: 0Last Post: 04162008, 10:55 PM 
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01132008, 09:13 PM
Bookmarks