Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

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

Greenhorn
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.

Ranch Hand
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
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
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
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.

Ranch Hand
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
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
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
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
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
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
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.