wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with arrays" Watch "Problem with arrays" New topic
Author

Problem with arrays

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
I have coded a program that, using an array, takes a series of doubles, finds their average, takes the number farthest away from the average, and deletes it. However, it won't find the average right. For data series: data[ 0 ] = 5.6
data[ 1 ] = 6.2
data[ 2 ] = 6.0
data[ 3 ] = 5.5
data[ 4 ] = 5.7
data[ 5 ] = 6.1
data[ 6 ] = 7.4
data[ 7 ] = 5.5
data[ 8 ] = 5.5
data[ 9 ] = 6.3
data[ 10 ] = 6.4
data[ 11 ] = 4.0
data[ 12 ] = 6.9

and program
It says that 5.5 is the farthest from the average, when it is really 4.0. Does any one know what I'm messing up? Thanks!!


John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Theresa:

Well, the easiest way to find the differences would be to take the absolute value of the difference between each data point and the mean (average), and the largest one should be easiest to calculate.

John.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Your problem can be fixed in two little steps.

First, create a static method that calculates the average of the array items.
To get you started, the signature should look like this.


The rest is a piece of cake.

Next, simply make use of the Math.max and Math.abs method and determine the value that is the farthest from the average.

JDBCSupport - An easy to use, light-weight JDBC framework -
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
another option to find the value furthest from the mean may be to sort the array - look in the Arrays class for how to do this.

Then you know that the furthest from the average is either data[0] or data[length-1].
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Thank you everyone! With a calumnation of your suggestions, I have managed to fix my program. Thanks again for your time!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Theresa Marlin wrote:Thank you everyone! With a calumnation of your suggestions, I have managed to fix my program. Thanks again for your time!


Care to share what you think calumnation means ?
I thought it was going to be a contender for new word of the day, but I can't find a definition for it. The closest I could find was calumniation - a malicious or knowingly false statement. I'm guessing this isn't what you meant.


Joanne
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
I'm sorry, I tend to make up words of my own that seem to make sense. Where I go to school, calumnation is a generally accepted word meaning combination ;) I forgot that I was talking to people all over.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
There are all sorts of "local" words; my girlfriend used a word once and was surprised that I didn't understand it. My wife (same woman) can still remember it and I can still use that word to wind her up.
 
Consider Paul's rocket mass heater.
 
subject: Problem with arrays
 
Similar Threads
Please Help!
How to "throw an exception?"
Multi dimensional arrays
when i run my virtual machine just keeps going, but no output.
A question about Scanner