/// GH solving PPPe-16

#include "..\std_lib_facilities.h"
#include <cstdlib> ///for random numbers
#include <ctime> ///for time()
/****************
16. In the drill, you wrote a program that, given a
series of numbers, found the max and min of that
series. The number that appears the most times in
a sequence is called the mode. Create a program that
finds the mode of a set of positive integers.
********************/

int main() {
    ///create a random vector of positive numbers between 0 and 17;
    vector<unsigned> rvec(18);
    srand(time(0));
    for(int i = 0; i < rvec.size(); ++i)
        rvec[i] = rand()%18;
    ///report out
    cout << "\nYour numbers are\n";
    for(int i = 0; i < rvec.size(); ++i)
        cout << rvec[i] << " ";
    ///create another vector containing a list of unique numbers
    ///from the first vector
    vector<unsigned> unqu;
    bool found = false;
    for(int i = 0; i < rvec.size(); ++i) {
        found = false;
        for(int j = 0; j < unqu.size(); ++j) {
            if(rvec[i]==unqu[j]) found = true;
        }
        if(!found) unqu.push_back(rvec[i]);
    }
    ///report out
    cout << "\nThe unique numbers are\n";
    for(int i = 0; i < unqu.size(); ++i) {
        cout << unqu[i] << " ";
    }
    ///create a vector counting how many times each number in
    ///second vector appears in the first vector
    vector<unsigned> kount(unqu.size(),0);
    for(int i = 0; i < unqu.size(); ++i) {
        for(int j = 0; j < rvec.size(); ++j) {
            if(unqu[i]==rvec[j]) ++kount[i];
        }
    }
    ///report out
    cout << "\nThe frequency count is\n";
    for(int i = 0; i < kount.size(); ++i) {
        cout << kount[i] << " ";
    }
    ///report the number in the second vector corresponding to
    ///the largest number in the third vector.
    cout << "\nThe sorted modes are: ";
    vector<unsigned> kountRaw = kount;
    sort(kount.begin(),kount.end());
    for(int i = 0; i < kount.size(); ++i) {
        cout << kount[i] << " ";
    }
    if(kount[kount.size()-1]!=kount[kount.size()-2]) {
        cout << "\nThe list has a mode of ";
        int i = 0;
        while(kountRaw[i]!=kount[kount.size()-1]) ++i;
        cout << unqu[i];
    }
    else cout << "\nThere is no mode.";
}
