I'm having trouble determining the mode. If the sequence is 9 3 3 3 9. The mode is found to be 9. It always tends to choose the higher value that's repeated even if it's less times. Any suggestions would be most helpful.
#include <iostream>
#include <vector>
usingnamespace std;
void read(vector<int>& myvector);
void mode(vector<int>& myvector);
int main()
{
vector<int> myvector;
read(myvector);
mode(myvector);
return 0;
}
void read(vector<int>& myvector){
int value;
cout << "Enter a sequence of number space delimited and q to end" << endl;
while(cin >> value){
myvector.push_back(value);
}
sort(myvector.begin(), myvector.end());
}
void mode(vector<int>& myvector){
int counter = 1;
int first, second;
int num = 0;
for (int i=1; i<myvector.size(); i++){
first = myvector.at(i-1);
second = myvector.at(i);
if (second == first){
counter ++;
num = first;
}
else
first = second;
}
cout << "The mode is: " << num << endl;
}