Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Problem with arrays

Ranch Hand
Posts: 49
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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: 74634
335
• Number of slices to send:
Optional 'thank-you' note:
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.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton