Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Still can't make a frequency array work  RSS feed

 
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again!

Given an input of cities, I have to print the number of cities Alice visited, without counting the duplicates. I did it first by copying the values in a HashSet:



This works fine. I also want to do a frequency array (because I am bad at that)

But this code won't work, I get 0 in the first array and 3 in the second!




Can someone find in her/his heart the patience to help with that ?
 
D.J. Quavern
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to say, it's not exactly the same code of course. In the second one I just want to print the frequency array to see how I populated it.

The problem description is:
https://open.kattis.com/problems/everywhere
 
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
D.J. Quavern
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
How can we make it only with primitive stuff? I mean only in[] arrays.
 
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array isn't a primitive; it is an object. I wouldn't think about simply using primitives, because you are going to dissociate the name of the city from the count. You can have a VisitCounter class with name and visitCouint fields (or similar). You can have an array of ints and count “random” numbers like this:-You can also collect (boxed) numbers into a Map<Integer, Long> with a combination of Collectors#groupingBy() and Collectors#counting(), but the resultant count will be a Long rather than an int.
 
D.J. Quavern
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you. That was advanced stuff.
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deja Quavern wrote:Wow, thank you.

That's a pleasure

That was advanced stuff.

Don't let people scare you into thinking the Streams API is more difficult than it really is. List
 
Carey Brown
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking closer at your problem to see how to get frequency out of your input. One thing I noticed is that you have two loops. An outer loop which is the number of trips and an inner loop that is the number of cities for a given trip. I prefer to separate responsibilities so rather than cram all the logic into these loops I'd break the responsibilities out, something like: input all trips/cities, print all trips/cities, calculate frequencies, print frequencies. Ideally you would put those responsibilities in separate methods. In order to separate responsibilities you'll need to start with an array of arrays where the rows are the trips and the columns are the cities for that trip.
 
Carey Brown
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example run:
 
D.J. Quavern
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your reply!

If you have the time and patience, can you please show me how to do the histogram?
 
Carey Brown
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to have a way to store the city name and its associate count. There are other ways to do this but it is highly recommended that you make a very small class that can hold a String and an int. String being the name of the city and int being a count of the number of times that particular city appears.

In the main program you will have to allocate an array to hold a number of these objects. How big an array? Well, the worst case scenario is that each city only appears once, so you'll have to go through the trips and add up the number of cities in each trip.

Now, if you have any city that appears multiple times the array won't be entirely filled in, so you need a counter to track how many in the array have actually been populated.

Then, traverse all the cities in all the trips and for each city look to find the entry in the array for that city. If you don't find one then add one. If you do find one then increment its counter.
 
D.J. Quavern
Ranch Hand
Posts: 124
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you and sorry for the late reply.
So you mean that everyone I enter a new city I have to traverse the array of the string-integer object (like artisanal hashmap, in other words?) to see if the string is already here?
How would I increment the count?
 
Carey Brown
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a linear traversal of an array to find a city is crude but it says within your requirements of sticking to basics like arrays. The speed is not as bad as you might think for reasonably sized data sets.

int count = 0;

// add new city
frequencies[ count++ ] = new Frequency( city ); // constructor count should default to '1'
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!