# Thread: [SOLVED] Calculating Std. Deviation using Java

1. Member 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***************************");```
Just to let you know but I need to have it in this format, but what I'm stuck on is the calculations part. Thank you =D  Reply With Quote

2. Member Join Date
Oct 2008
Location
Washington, US
Posts
28
Rep Power
0

## Never mind I solved it  Reply With Quote

#### Posting Permissions

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