Hello lastchance!
My book, Chapter 9, (which is Starting Out With C++, From Control Structures to Objects (a fantastic book!), in essence, says the following:
The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return.
There is no mention of more than one mode and how to handle the situation (Maybe the author thought this is too difficult? Who knows). So when I started working on this one, I did my "homework". Which functions to use, what each should do, writing a small driver program to test things in small scale, so on so forth. What I didn't do this time around, as things seemed clear enough, is doing some research on the topic. This, I guess, is what I like most about learning to code in C++ (which, of course, I don't learn for the sake of learning it, but for a purpose) - I learn a thousand things besides writing code, making it all the more worthwhile to solve every problem the book has to offer. But I digress.
Of course I did some sort of research, meaning that I soon found out about the possibility of there being more than one mode. So I wanted to take this fact into account, which, without - would just make my program feel incomplete. This is also where my problem started ... The reason being that, for whatever reason, I started thinking that if all numbers in a set are equally distributed, there can be no mode. The first question you asked is the best example: What are the mode(s)?
The answer came easy: 1 2 2 3 3 4 4 5 has three modes, because 1 and 5 are both in there only once, while 2, 3, and 4 are present twice each, making it three modes. In the same vein I was looking on my problem, which led to my conclusion that:
A set consisting of these numbers 3, 3, 2, 2, 1, 1 has no mode at all, because they are equally distributed, and no number is higher or lower. The long and short of it is: No number higher or lower, no mode, return -1, get rid of it all (which I failed to do.) Now you are saying, and I allow myself to quote:
I'm afraid that I (and all my colleagues) would say that this situation simply had several modes. We quite regularly talk about bimodal distributions, for example. |
And this is exactly the answer I was looking for, solving all my problems! And just to make absolutely sure that we are on the same page on this: 2, 2, 3, 3 = bimodal, 2 modes? Which is also what your code returns as a result when given the following input: 3, 3, 3, 2, 2, 2, (Modes are 3, 2), as does calculator-soup, and some other online calculator sites offering to find median, mode, mean, ...).
-
I'm afraid that I would struggle to write sample code in your style (and I'm afraid I don't fully follow your version with pointers as it is unclear to me whether frequencies have already been counted or are being calculated in the routine) |
And I would never ask for that (or any code for that matter, as it is my goal to solve the problems on my own, as this is - after all, the purpose of learning. Going by the hints being offered when I struggle and have to ask.) I only published the code, not only of course to maybe get an answer, but also to show that I did all the work, and not simply ask: "Please solve a problem, give me code ..." But I would still like to explain my function and what it does, and how it does it.
Before the function to determine the mode(s) is entered, the virus array is sorted in ascending order. The first while loop compares the virus array to itself, and increments
count each time an equal number of numbers is found. Meaning 1, 2, 2, 2, 3, 3, 3, 5 - and, upon exiting this first loop, this count then is stored in the appropriate position in the frequency array, looking like this 1, 2, 3, 0, 0, 1.
In the if statement that follows, all the rest of the work is done, in determining the highest frequency if there is any.
frLow is assigned
frequency[index] containing all the counts, and
mode is assigned virus, signifying the highest number that is encountered in frequency, based on the index. (I hope this explanation makes sense. :))
frequency in this function, and on the whole, should serve the following purposes:
1: To keep track of count
2: To find the mode
3: To determine whether there is A mode. (This comparison should happen in a second if-statement.) And if there is no mode (going on the believe that - as mentioned - 1, 1, 2, 2, 3, 3, == no mode, should simply return -1).
4: To create a histogram in another function
Once all this is done, the correct mode should be returned.
It finds the mode, or modes, and the results have been correct so far. If there is any more than one, this isn't taken into account by the function. It only ever returns
the most frequent one. I will alter this, so that if there is A mode, then this is returned and displayed accordingly, and if there is more than one, I use the frequency array to display all of them in a separate function.
In essence I do not wish to discount anything but the highest mode. If there is more than one the output to screen should reflect this fact. Anything else I would consider making my program seem incomplete.
As to your suggestion about using a map, I haven't learned that. The farthest I have come is pointers, next lesson being *lemme think* Characters, C-Strings, and I believe what is called String class. But there is some more problems to be solved, among which finding the median and average. ;-)
After this wall of text, I'd like to sincerely thank you for answering my question, which has helped me getting rid of a problem which never was. (Until I managed to turn it into one ....). You be sure to be given proper credit for your invaluable help!