wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes please help figure out why the standard deviaion is not giving the correct output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help figure out why the standard deviaion is not giving the correct output" Watch "please help figure out why the standard deviaion is not giving the correct output" New topic
Author

please help figure out why the standard deviaion is not giving the correct output

unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Hi., i'm reading integers from a file. i've been to read input from the file and also calculate the average, but the standard deviation is yielding the wrong output, help please.what should i change or add.





Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Unyime,

What input did you provide and what result did you get?

I tried running your calStandardDev() by giving some values.


num = 1, 2, 3
average = 1
standardDev (result) = 1.2909944487358056 (Is this the expected result?)

Joyce
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Hi joyce,
thanks for your time. from 1,2,3 the average is suppose to = 2(i.e 1+2+3/3)
while the standard dev = 1.
I'm reading my input from a file and the values are
45
5
53
7
88
32
2
1
5
The results i get are:
The Average is: 26.444444444444443
The Standard is: 26.789359534381877

Average is correct, but standard dev is suppose to = 30.4389188


Below is the class i'm reading the file from:

Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hello Unyime,

Okay, I got it now. Here is the correct version.



Output is 30.43891880106417

Standard Deviation Formula

Joyce
[ November 14, 2004: Message edited by: Joyce Lee ]
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
i don't know why i don't get the correct output over here, probably because i'm reading from a file. i've tried everything or could it be the editor i'm using? i tried running with NetBeans IDE and Drjava, but i still get wierd answers for the standard dev. Anyway, thanks for your time and concern Joyce.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I don't know if you've fixed it yet or not, but there is a bug in the original code posted above:As you can see from my comment, you are reading in a new number and overwriting the last element of the array (since x was left pointing to it). Thus, the standard deviation is calculated from a different set of numbers than the average.

[ Grammar Gremlin ]
[ November 14, 2004: Message edited by: David Harkness ]
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Hi David,
I've not yet fixed, I just realized that the problem is from my readInteger() , so i'm trying to figure out how to fix that.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by unyime inok:
inString.trim();
The trim() method doesn't modify the String as all Strings are immutable. Instead, it returns a (possibly new) String with leading and trailing whitespace removed.

How are you using the other number you're reading? Why do you have to put it into the array?
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
the specification requires that the numbers be read and stored into an array, then use that to calculate the average and standard dev. Now the only problem is that the zero that gets returned if the end of file has been reached gets stored also, increasing the length of the array, thus giving an incorrect output for the standard dev.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Normally you want to stay away from "special" return values, but it is a common tactic. You could choose Integer.MIN_INT as the "no more numbers to read" value instead of zero. Then only add the number to the array when the result of readInteger isn't that value.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Unyima, the calStandardDev() method I used is based on your previous thread which did not use in.readInteger() in the method.
[ November 14, 2004: Message edited by: Joyce Lee ]
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
the calStandardDev() works if you're assigning values to the array directly or reading from the keyboard, doesn't work if you're reading from a file. once i fix the readInteger() it should be fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help figure out why the standard deviaion is not giving the correct output