OK, I've done a lot of work and got it to work, well mostly. It is a way different format than what i had before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
double mode;
vector<double> v;
vector<int> vOccurrence;
set<double> occurSet;
copy(istream_iterator<double>(cin), istream_iterator<double>(), back_inserter(v));
sort(v.begin(), v.end());
vOccurrence.resize(v.size());
for( vector<double>::const_iterator iter = v.begin(); iter != v.end(); ++iter ) {
sum = sum + *iter;//used for mean
// vOccurrence will be used later to find the mode
for( size_t i = 0; i < v.size(); ++ i) {
if( *iter == v[i] )
++vOccurrence[i];
}
}
|
The code above runs through the user's input vector(v), and at each iteration of that vector(v) it will go through the vector(v) again checking how many time a number occurs and storing the occurrence in another vector(vOccurrence).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// Mode
for( vector<int>::const_iterator iter = vOccurrence.begin(); iter != vOccurrence.end(); ++iter ) {
if( *iter > maxOccur ) {
maxOccur = *iter;
}
}
for( size_t i = 0; i < vOccurrence.size(); ++i) {
if( vOccurrence[i] == maxOccur ) {
mode = v[i];
occurSet.insert(mode);
}
}
cout << "\nMode =";
for( set<double>::const_iterator iter = occurSet.begin(); iter != occurSet.end(); ++iter )
cout << " " << *iter << endl;
|
The code above runs through the vOccurrence vector finding the biggest number, which would be the most occurring number. Since both v and vOccurrence are the same size both their elements correspond.
Meaning: v[0] and vOccurrence[0], v containing the users first input, vOccurrence holding the number of occurrences there were.
But since vOccurrence would have multiple doubles i put the highest occurring user inputs into a set, getting rid of doubles. So this will output a single mode, or if there are multiple, it will display multiple.
i.e. users input: 1 1 2 3 4 4 5 6 7 7 output: Mode = 1 4 7
With that ironed out i still have to display the possibility of no mode. As it stands now if the user inputs: 1 2 3 4 the output will be Mode = 1 2 3 4. Which is not correct. Again any help is appreciated!