I am trying to write a program to calculate mean & standard deviation.

I sort of figured out how to find mean (it is not correct yet, so i am going to need help on this part as well) but how do you do standard deviation?

``````import java.util.Scanner;

public class Mean {
public static void main(String[] args){
int sum = 0, inputNum;
int counter;
float mean;
Scanner NumScanner = new Scanner(System.in);
Scanner charScanner = new Scanner(System.in);

System.out.println("Enter the total number of terms whose mean you want to calculate");

counter = NumScanner.nextInt();

System.out.println("Please enter " + counter + " numbers:");

for(int x = 1; x<=counter ;x++){
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
}

mean = sum / counter;
System.out.println("The mean of the " + counter + " numbers you entered is " + mean);
}
}
``````

The above is what i have so far, there are errors but hopefully we can build from there?

@AD, i know what is standard deviation and how to find it but i wasn't sure how to do it and implement in java.

Do you know how to implement it within my code?

@ <M/>:
You know how to find standard deviation.Then just write an algorithm or make a flowchart and implement it in a for loop, as you have done to find the mean.
It seems that you have'nt tried.

Just in a for loop, subtract each element from the mean, you found and then add the result in a variable.
And out of this loop divide it with counter and just find the square-root of it.

You know how to find standard deviation.Then just write an algorithm or make a flowchart and implement it in a for loop, as you have done to find the mean.
It seems that you have'nt tried.

not exactly, i have tried but apparently i can't take it from paper to code that easily.
I am going to see what i can come up with in the background.

okay.....then post here what you tried.

there are quite a few flaws but finding the mean is pretty close (i need to figure out rounding on java) but standard deviation is way off.

This is what i got:

``````import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
System.out.println("The standard deviation is " + deviation);
}
public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
return sum / (x.length - 1);
}
}
``````

But the you forgot to use under-root of (sum/x.length-1).

Even if you get stuck , trace your values with example given here:
http://standard-deviation.appspot.com/

So doing it like this works but for some odd reason I can not do a decimal format very well...? This should be the last correction for this questions, thank you guys!

``````import java.text.DecimalFormat;
import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
System.out.println("The standard deviation is " + deviation);
}
public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
return sum / myFormatter.format((Math.sqrt(x.length - 1)));
}
}
``````

Because I think formatter won't change the original value,and You are pinting original value in main().

Try this:
In main() where you are printing 'deviation',

``````DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
System.out.println("SD is : "+ myFormatter.format(deviation));
``````

The calculation of your standard deviation is correct, however, the return statement is wrong hence giving the wrong value.

Sample Standard Deviation = square root of(sum of(squared(x - mean))/N-1)

What you have done is sum of(squared(x-mean)/ square root of(N-1)

Do not decimal format in the `var` method. Since `format` returns a string value and the method needs a `double`.

Just return the calculation of the standard deviation without formatting.

So do the decimal formatting in the `main`.

``````public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("The standard deviation is " + df.format(deviation));
}
``````

Hope that helps.

I tried applying that, I still got an error? This is what i did:

``````import java.text.DecimalFormat;
import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("The standard deviation is " + df.format(deviation));
}

public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
return sum / myFormatter.format(Math.sqrt(x.length - 1));

}
}
``````

At line 38 remove myFormatter.format()

So your return statement should look like this:

`return sum / Math.sqrt(x.length - 1);`

However, that is not correctly calculating standard deviation. Read my previous post about the correct way to write your return statement.

So like this:

``````import java.text.DecimalFormat;
import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
System.out.println("The standard deviation is " + deviation);
}
public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
return sum / myFormatter.format(Math.sqrt(sum of(squared(x - mean))/N-1));
}
}
``````

When I said "sum of" I meant it as pseudocode and mathematical expression.

This is what I wrote the correct calculation:

Sample Standard Deviation = square root of(sum of(squared(x - mean))/N-1)

`return Math.sqrt(sum/(x.length-1));`

The sum is "sum of(squared(x-mean)" and (x.length-1) is N-1.

And I see you removed the decimal formatting from the main.

So like this:

``````import java.text.DecimalFormat;
import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
System.out.println("The standard deviation is " + deviation);
}
public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
return Math.sqrt(myFormatter(sum/(x.length-1)));

}
}
``````

You will be getting an error at line 36.
I think we are going in circles here.

Your main will be like this:

`````` public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("The standard deviation is " + df.format(deviation));
}
``````

Your `var` method will be like this:

``````public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}

return Math.sqrt(sum/(x.length-1));
}
``````

Okay, so like this:

``````import java.text.DecimalFormat;
import java.util.*;
public class Mean {
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double[]x = new double[10];
int i;
for(i = 0;i < 10; i++){
x[i] = s.nextDouble();
}
double mean = mean(x, i);
double deviation = var(x);
System.out.println("The mean is " + mean);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("The standard deviation is " + df.format(deviation));
}
public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static double mean(double[]x, double i){
if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}
public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}

return Math.sqrt(sum/(x.length-1));
}
}
``````

but what about the decimal format for Standard Deviation?

but what about the decimal format for Standard Deviation?

At line 15.

Have you tried running the code?

Yeah, it only gives me the decimal format for mean?

Are you sure?

Can you show the inputs and output?

Here is the error log that i got:

``````First Difference Occurs at: byte 15, line 1
On Line 1 its column 15
Line 1 Wrong: The mean is 30278.519905400004
Line 1 Right: The mean is 30,278.52

sdiff side by side difference your output versus solution....
The mean is 30278.519905400004                    | The mean is 30,278.52
The standard deviation is 52281                   | The standard deviation is 52,281.00
``````

hey your code in comment no:9 will work correct,if you remove decimal-formatter from the var() function and return normal value.And in main function,add what I said in comment No:10

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.