This is how it would work for data set of doubles between 0 and 1
Let's say that we have the following data set:
0.1 0.2 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.6 0.9
|
Then, the first double vector,
|
std::vector <double> numbers;
| |
would store
0.1 0.2 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.6 0.9
|
The second int vector,
|
std::vector <int> occurrences;
| |
would store the counts of each value in the first vector (In the corresponding index)
Then, find the largest count in the occurrences and store it in
So, in this case, maxOccurrence would be 5
Then, finally, the third vector,
|
vector <double> storeModes;
| |
would store all values from the first vector that have a count of 5 in the corresponding index of the second vector
Of course, I would want to store 0.4 only once, so I included the following control at Line 47
|
if ( *std::find ( storeModes.begin() , storeModes.end() , numbers.at(i) ) != numbers.at(i) )
| |
Basically, before a value is added to the storeModes vector, the program will check the elements in the storeModes for the same value. If the same value is found, then it won't add the same value again
So, finally, the storeModes vector would have the following
, which is the mode for this example data set.
I hope that answers your question.
I am sure there is a better way to program this. I am not so experienced with C++ or problem-solving. I came up with this by myself and I am looking for ways to improve the codes.