I’ve been having issues with this specific problem in Java. I’m stuck!
We ask from the user to enter number of data & then enter the temperatures and the names of the cities respectively.
It is said though that the user might enter multiple temperatures for a specific city, for example:
Vienna 23 (in C)
Berlin 12
Paris 15
Berlin 9
Berlin 10
Paris 10
in no specific order. I’ve done this part with two 1D arrays but then the program asks that we calculate the average temperature of every city, the highest and the minimum temperature (obviously the min/max/avg will be the same if we only have one temperature of a city).
I don’t know how I should do this part considering I have to go through the cities array and check how many times a specific city is listed there and then find the respective temperatures to find the average/min/max.
I have the concept but I can’t make it in Java. Any thoughts/suggestions?
Much appreciated!
You need to change your
Data Structure. Using two different arrays for attributes that are related is not a good idea.Rather, maintain a mapping between city, and all its temperature. You would need to create a
Maplike this: –Then for each city, you can easily calculate the average using the
List<Double>for each of them.