Results 1 to 15 of 15
 08302010, 02:56 PM #1Member
 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 / (101)); System.println(" The standard deviation is" + dev); } }
 08302010, 03:03 PM #2Member
 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.....
 08302010, 04:07 PM #3
If you have errors, please copy and paste the full text of the error messages here.
 08302010, 07:49 PM #4Member
 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 / (101)); System.out.println(" The standard deviation is" + dev); } }
 08302010, 08:21 PM #5public static double deviation (double [] x) // error here saying it should return a double
Add a return statement or replace the double with void
 08302010, 09:59 PM #6Member
 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 / (101)); // maybe n should go here? System.out.println(" The standard deviation is"); return dev; } }
 08302010, 10:07 PM #7says my program is not complete
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.
 08302010, 10:20 PM #8Member
 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)
 08302010, 10:28 PM #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
get the standard deviation of that same 10 digit array
the program would not compile in the tester
That makes it harder.
 08302010, 11:06 PM #10Member
 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.

If anything about his questions confuse you, please ask.
If there is nothing to find further, then that is ok. I thank you for looking anyways.
 08302010, 11:18 PM #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;
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 10Last edited by Norm; 08302010 at 11:21 PM.
 08312010, 12:01 AM #13Member
 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 / (101)); // 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.
 08312010, 12:09 AM #14I take the square root of (sum  array length)
Math.sqrt(sum / (101))
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 = 1Last edited by Norm; 08312010 at 12:12 AM.
 08312010, 12:51 AM #15Member
 Join Date
 Aug 2010
 Posts
 70
 Rep Power
 0
Similar Threads

Redirect standard output
By Cylab in forum New To JavaReplies: 3Last Post: 08072010, 04:35 PM 
Logging Standard output to a file
By jhyuk in forum Advanced JavaReplies: 2Last Post: 11092009, 11:31 PM 
standard deviation and mean for an array
By peterdfl in forum New To JavaReplies: 3Last Post: 05292008, 04:09 AM 
Sun Java Platform, Standard Edition 6u6
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04262008, 08:26 PM 
Sun Java Platform, Standard Edition 6u3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10042007, 09:10 PM
Bookmarks