# Thread: Finding mean and Standard Dev - fairly simple

1. Member
Join Date
Aug 2010
Posts
70
Rep Power
0

## Finding mean and Standard Dev - fairly simple

Hey guys -

I have the code mostly done ( I think), but the error I have is that the Println at the end says that it is a string and I am not doing it properly. I am assuming I have to import the Scanner class/thing to make this work, right?

Java Code:
```public class Statistics {

public static double mean (double [] x)
{
double sum;

double average;

if (x == null)
throw new IllegalArgumentException("null array");

for (int count = 0; count < 10; count ++)
{
double number = x[count];

sum += number;
}

average = sum / 10;
System.println(" The average is" + average);
}

public static double deviation (double [] x)
{
if (x == null)
throw new IllegalArgumentException("null array");

double sum;
double average = mean(x);
double dev;

for (int i = 0; i < x.length; i++)
{
sum += Math.pow((x[i] - average),2);

}

dev = Math.sqrt(sum / (10-1));

System.println(" The standard deviation is" + dev);

}
}```
Thanks a million for your set of eyes on this!

2. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
Sorry, I got it. Only errors so far is that sum and average are not initialized, but I got the system.out.println thing.....

3. If you have errors, please copy and paste the full text of the error messages here.

4. Member
Join Date
Aug 2010
Posts
70
Rep Power
0

## Update

This is what I have - and I will post the errors to make things easier. Sorry about not doing that before. Errors are in the method headers.

Java Code:
```public class Statistics {

public static double mean (double [] x) // error saying should return doubl
{
double sum=0.0;
double average;

if (x == null)
throw new IllegalArgumentException("null array");

for (int count = 0; count < 10; count ++)
{
double number = x[count];

sum += number;
}

average = sum / 10.0;
System.out.println(" The average is" + average);
}

public static double deviation (double [] x) // error here saying it  should return a double

{
if (x == null)
throw new IllegalArgumentException("null array");

double sum = 0.0;
double average = mean(x);
double dev;

for (int i = 0; i < x.length; i++)
{
sum += Math.pow((x[i] - average),2);

}

dev = Math.sqrt(sum / (10-1));

System.out.println(" The standard deviation is" + dev);

}
}```

5. public static double deviation (double [] x) // error here saying it should return a double
You have defined the method to return a double, but you forgot to add a return statement to the method.
Add a return statement or replace the double with void

6. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
Thanks Norm. I redid it - is there a way that either you or someone can just check the logic? My professor gave me a tester program that says my program is not complete - but my program shows no errors.

I dont mean to be a pain, but if someone can find what is wrong, I would appreciate it. you guys have already helped so much!

Java Code:
```public class Statistics {

public static double mean (double [] x)
{
double sum=0.0;
double mean;

if (x == null)
throw new IllegalArgumentException("null array");

for (int count = 0; count < 10; count ++)
{
double number = x[count];

sum += number;
}

mean = sum / 10.0;

System.out.println(" The average is");
return mean;

}

public static double deviation (double [] x)
{
if (x == null)
throw new IllegalArgumentException("null array");

double sum = 0.0;
double mean = mean(x);
double dev;

for (int i = 0; i < x.length; i++)
{
sum += Math.pow((x[i] - mean),2);

}

dev = Math.sqrt(sum / (10-1)); // maybe n should go here?

System.out.println(" The standard deviation is");
return dev;

}
}```

7. says my program is not complete
What does being "complete" mean? For example I don't see any GUI to get input or any file I/O methods to read data from a file, etc ...
If you would add comments to the code describing exactly what it was to do, and how it was going to do that, someone could look at those comments and see if the code does what the comments says it is supposed to do.

Comments on the code as shown.
You control the for loop with a hardcoded literal 10 in one method and use the length attribute in the other. The latter method is better.

8. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
I was to create two methods:

1) get the mean of a 10 digit array

2) get the standard deviation of that same 10 digit array

When I said "complete" , it may have been better to say that the program would not compile in the tester (test program I have to use to prove if my program will work)

9. You've given me the problem statement, not a description of how YOU are going to solve the problem.
1) get the mean of a 10 digit array
Describe the programming steps to solve that problem

get the standard deviation of that same 10 digit array
Ditto. How is this to be done in your program?

the program would not compile in the tester
No error messages? You only get success or failure?
That makes it harder.

10. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
I have two methods in the program I have posted.

The first method is to return the mean of a ten digit array.

the second method is to return the deviation of the same ten digit array.

When you ask what I have done, I have posted it all so far.

using math, the formula for the mean and deviation, I have created the two methods posted.

I might be a little confused at your question , so I apologize if I am not being more specific.

If there is nothing to find further, then that is ok. I thank you for looking anyways.

I might be a little confused at your question , so I apologize if I am not being more specific.

If there is nothing to find further, then that is ok. I thank you for looking anyways.
There's plenty more to find, but you should start by answering Norm's questions (or seeking clarification if they confuse you). Your answers will help us to help you.

12. Hear is an example of how to describe the steps the program takes.
Java Code:
```	for (int count = 0; count < 10; count ++)		{
double number = x[count];
sum += number;
}
mean = sum / 10.0;```
The steps this code does is:
Loop 10 times (this should be from the aray's length attribute, not a literal 10
get the next number from the array x indexed by count
Add that number to the sum
end loop
Divide the sum by the number of elements in the array (again use length not 10
Last edited by Norm; 08-30-2010 at 11:21 PM.

13. Member
Join Date
Aug 2010
Posts
70
Rep Power
0

## Response

Java Code:
```public static double deviation (double [] x)
{
if (x == null)
throw new IllegalArgumentException("null array");

double sum = 0.0;
double mean = mean(x);
double dev;

for (int i = 0; i < x.length; i++)
{
sum += Math.pow((x[i] - mean),2);

}

dev = Math.sqrt(sum / (10-1)); // maybe n should go here?

System.out.println(" The standard deviation is");
return dev;```

Ok here we go.... If I mess up this attempt, apologies.

I am trying to find the standard deviation of the ten digit array. I loop through the array (noted by "length" of array and use the counter) .

Then I will use the definition of the deviation as the elements in the array minus the mean (previous method) squared. I put all of these elements into a variable called sum.

To finish the equation, per computer terms, I take the square root of (sum - array length). This is the deviation that I was looking for.

I then am trying to return the value of deviation at the end, so anyone can input a 10 digit array (random numbers) and the screen will print out "Deviation is: X"

I hope that helps some. Thanks again for your patience.

14. I take the square root of (sum - array length)
vs
Math.sqrt(sum / (10-1))
This code takes the square root of (sum divided by 9) (9 = 10 - 1) ????
??? Why the hardcoded literals 10 and 1? vs x.length
The division is integer which means there are no decimal points: EG 10/7 = 1
Last edited by Norm; 08-31-2010 at 12:12 AM.

15. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
AH. I see it now. Thanks Norm.

I have read on the internet that sometimes it is better to do (n-1) when doing the standard deviation instead of the other way.

You are the best. Thanks for helping me figure that out. I cant believe it was such a dumb mistake........... ahhhh...... java...........

#### Posting Permissions

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