I turned the assignment in today, and the professor rejected the way I did it. Basically she told me that it was the most inefficent way possible, and to use a frequency array.
So can someone explain exactly what a frequency array IS (she didn't do a very good job =p), so I don't spend 2 hours on something that should take 30mins?
10-31-2005, 03:05 PM
Its an array of frequencies I presume, but I dind't know that it was a defined
algorithm concept. The solution I presented at the bottom of page 1 of this thread
uses such an array and it only scans the charsequence once. You can't do less
scanning than that if you want to get the job done.
The bad thing about that solution is that it is far from an object oriented
approach, it's just "grunt" sequential programming that has resulted in a
concrete block of code.
The other solution I presented (the long winded one,... remember ? :) ) is in
my opinion a far better solution.
10-31-2005, 03:55 PM
I would use that approach, however the teacher insists that I use a "frequency" array. She wants it to look something like this:
Somehow she insists I can make an array of 26 counters, and store the "frequencies" of the letters there..and then output the most frequent one. Although I'm not sure how to store the letters in the array, and keep track of their count(s).
10-31-2005, 07:11 PM
The last solution I posted in this thread does exactly that, and it will
work for other alphabets also, not just the 26 characters that seems to
be the only characters in your teachers universe.
My solution uses an int array of 256 ints, all initially 0. I see no point in
fiddling with a subset of 26 or whatever, it just clutters up the code.
As I scan the characters I use each character (integer cast) to select the
correct int in the array and increment this by 1. When done I just scan
the 256 ints for the highest value. The index of the highest value is the
character with the highest frequency.