• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with arrays

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone! With a calumnation of your suggestions, I have managed to fix my program. Thanks again for your time!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic