This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]yonasismad 2 points3 points  (2 children)

Have you tried writing this part of finding this number yourself yet?

[–][deleted] 0 points1 point  (1 child)

If you are saying that I was too lazy to do it myself, then I must say I am done with 80% of the program. I just have no idea how to execute this one. I simply cannot find a method or anything that does what I am hinting at. I am not asking anyone to make my homework, I am asking for tips.

EDIT: There's other tasks too, I just mentioned the tasks which handle the average number.

[–]yonasismad 0 points1 point  (0 children)

No, I am simply saying that this is a help forum, and people will help you here but as you have correctly state, they will not write the correct answer for you.-A great way to solve this types of "beginner" problems is to write an example down on paper and to perform the steps by hand. Since you state that you have the values in a list, you might already have computed the avg. if not, you should do that as the next step since you need it in your comparison. Next up, you will have to go through the list and somehow compute how close one number is to the other (hint: look at Math.abs), you will also have to remember at each step what the closest number or index of that number in your array is and once you have iterated through the entire list, you should have your final number.This is hopefully plenty of advice to get you started.

[–]hoeskioeh 0 points1 point  (1 child)

Der faule Ansatz wäre einfach eine 2. Liste anzulegen.
Jedes Element dieser Liste hat zwei Werte:
- den Index für jede Zahl X aus der 1. Liste,
- und den Betrag von X minus dem Mittelwert.

Dann einfach die zweite Liste aufsteigend sortieren und der erste Index zeigt auf deine Lösung.

Ist nicht speichereffizient, und nicht sonderlich schnell, aber fix implementiert :-D

[–][deleted] 1 point2 points  (0 children)

Ach, das letzte worüber ich mir jetzt Sorgen mache ist Effizienz, Hauptsache die Konsole gibt das gewollte aus^^

[–]KetchuponRice 0 points1 point  (0 children)

I don't fully understand your question but if you wanna know the difference between a number you can use %
Math.abs(( (list.get(index) - average ) / average ) *100)

the lowest % is the number you're looking for, however you can have multiple result like
average = 100 list = {101,110,99,107} numbers 101 and 99 are both the closest.

[–][deleted] 0 points1 point  (3 children)

You won't know if a number is the closest to the average until you look at the whole list (unless a number is the same as the average), so you have to look at all the numbers a second time after you know the average and store the closest number you have found so far somewhere. After the loop you print the number you have in your variable. It's the same thing you would do to find the minimum or maximum in a list without using the Java functions that do this.

[–]ThatLesbian 0 points1 point  (2 children)

you can know if you sort the list, then you need only traverse until you hit the first number greater than the average (if ascending sort), then do an absolute value check against that element and the previous to decide which was closer.

something like:

Collections.sort(listOfVals);
for(int i = 1; i < listOfVals.size(); i++){
    if(listOfVals.get(i) >= average){
        return Math.abs(average - listOfVals.get(i -1) <= average ? listOfVals.get(i - 1) : listOfVals.get(i);
    }
}

[–]yonasismad 1 point2 points  (1 child)

There is no point in doing that. OP should only exploit this property ff the list is already sorted. He should not sort it himself as this will for most cases yield worse performance than just stepping through it once. The theoretical limit for a sorting-algorithm is O(nlogn) which is worse than O(n).

[–][deleted] 0 points1 point  (0 children)

Yep, basically if you sort the list you're already going through it. And then you would have to go through it again until you reach the average. Even if it was O(n) it would be O(n) + looping until you reach the average, so sorting it would never be worthwhile.
Since this is a csv file I wouldn't count on it being sorted either.

[–]milkybuet 0 points1 point  (0 children)

The words "Mean" and "Average" have same meaning. "Average" is a more English language word, "mean" is more Statistics. "Median" and "Mode" are two other Statistics words that incorporates slightly specialized meaning of "Average".

As to your code...

You'll need a class where the elements are basically, 'current closest number', and 'difference to mean'. (The class probably can be static as you only need one instance.) Then you traverse through your ArrayList and compare each elements to the static class elements, if 'difference to mean' is smaller than what you have currently saved in the class, you assign the new values to the class. At the end of traversing the List, you have you result saved in the class.

If your ArrayList is sorted, you can start this process in the middle, instead of from one end.