Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Compare to an Average Value - The NEAREST Value

Declan Dee
Greenhorn
Posts: 20
Greetings Gents,

Quick question.

Imagine this. I have had to create an arraylist with 5 different objects. These 5 different objects are countries. Each country has different information based on the constructor.

One of these is an double which is the density of the country. It can be whatever, that does not matter. I have already created a method that goes through each object takes out the required information that calculated the density. I have also created a method that calculates the sum of all the densities for each country and followed by the average density of each country. Now I have been asked to take the density of each country and compare that to the average density of all countries and print off which is the nearest to the average. I dont have a clue!

I can find the higest value and the lowest but how do I find which value is closest to the average.

So for example, I have 5 countries with the following densities calculated (not true of course)

Country1 = 2 / Country2 = 4 / Country3 = 6 / Country4 = 8 / Country5 = 10

The total is 30 and the average is 6.

Country3 is selected becacuse it is closest to the average. In this case it is the average (concidence)

I will continue to work on this. If anyone can help. Its appreciated

Declan

EDIT : I just spoke with my instructor. She gave me some help on the logic side. She said to create two variables, one to save the name of the country and one to save the difference, then I can compare better....Hmmmmmm

Joanne Neal
Rancher
Posts: 3742
16
You say you have written the code to find the country with the lowest density. All you have to do is modify this to find the country with the lowest difference between its density and the average density. To calculate the difference you simply subtract the average density from the country's density and then get the absolute value of the result (the Math class has a method that will do this for you).

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Joanne is too nice.

I would have made you explain to me HOW YOU KNEW that country 3 was the closest. You had to do something in your brain. The trick to writing software is converting what your brain does to what the computer does.

Vineet Kakati
Greenhorn
Posts: 21
As your instructor said create another variable for eg 'diff' that stores difference;

1:Find the absolute difference between density of each country and average density and store the difference in diff;

2:if diff is less than stored value of diff
then store the index of that country in another variable for eg 'index';

Repeat steps 1 & 2 for all countries in array.

At end 'index' will be the index of country in array having density nearest to average

Campbell Ritchie
Sheriff
Posts: 50225
79
I would suggest the "diff" variable ought to be a local variable to the method, not a field. In your present example, you will have no problems, but there might potentially be problems if there are many values close together, because of the precision or otherwise (see No 20) of the floating-point arithmetic.

Declan Dee
Greenhorn
Posts: 20
Hi,

Thanks for all the answers and help!

The players have changed but the game has stayed the same. This time I am now calculating which countries residents are closest to the average. Logic stays the same of coruse

This is the first solution outside of a loop. Please note that 'inwoners' means residents and 'landen' means countries

Now I have to put the second part in a loop (even though the first part is already done)

Campbell Ritchie
Sheriff
Posts: 50225
79
Get all that code out of the main () method. The main () method is intended for starting off your application, not for doing all the work.
Your List should be parameterised eg List<Country>, then you can get rid of that nasty class-cast.
You need to iterate through the List, and if you find a value closer to the average, you can reduce your diff value and record the Country you found.

Declan Dee
Greenhorn
Posts: 20
Hi Ritchie,

Indeed you are right that should not be in the main method and it shall be removed later. I shall also ask my instructor about 'parameterising' my List

Ok, I managed to solve my loop issue. My logic was to assign a default value and default land and compare the rest (the other countries) to them. This is my finished article. It may not be everyones cup of tea, and it may require a clean up, but I am quiet happy as it is my own logic

The first code shows my finished article. The second shows what Ritchie mentioned in anonther post about casting. See line 11 in the second example.

Campbell Ritchie
Sheriff
Posts: 50225
79
Only had a half-second look at it, but it looks good

You should have methods like calculateAverageXXX and findClosestToAverageXXX. Whether the average is a field of the class or a local variable is a point for discussion; I'll let you work it out first before I tell you what my preference would be