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
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

# Problem with arrays

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

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

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

Ranch Hand
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
Posts: 49
Thank you everyone! With a calumnation of your suggestions, I have managed to fix my program. Thanks again for your time!

Rancher
Posts: 3742
16

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.

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

Marshal
Posts: 64642
225
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.

 It is sorta covered in the JavaRanch Style Guide.