// Exercise to find the most frequent numbers in an input #include #include #include using namespace std; void count_inputs(int min, int max, map &frequencies); // TODO: complete the following prototype so the map is passed by const reference // Name this count frequencies. // This map captures how often a particular number appears in the input; that // is, for each distinct input value there will be a count of the number of // times that value appears in the input. void report_most_frequent_items(); int main() { int min, max; cin >> min >> max; // TODO: write code to declare the map containing frequency data // (for each distinct input value, the number of times that value // appears in the input) and call count_inputs to read the // data and count the frequencies return 0; } void count_inputs(int min, int max, map &frequencies) { // TODO: implement the following pseudocode: // Read all numbers from input (this is an end-of-file loop reading ints) // For each, if the number is out of range then print // Value X is out of range. // otherwise, increment the count for that number in frequencies // NOTE: use [] to access elements of frequencies; in this way, // this takes advantage that frequencies[765] will be 0 if you // this is the first 765 the program encounters // DO: Follow the standard model for an end-of-file loop: // read // while more data // process // read } // TODO: copy the parameters from your prototype to here void report_most_frequent_items() { // This function creates a sorted list of all distinct frequency counts and // then prints the most frequent items from that list. // For example, if frequencies has just three entries because 3 appears 12 times // and both 104 and 99 appear once, the list would be [12, 1] or [1, 12] // See the following TODOs to implement this. // TODO: declare counts to be a list of integers: for(auto f : frequencies) { int frequency_of_this_value = f.second; // TODO: next, add frequency_of_this_value to the end of counts } // TODO: call the sort and unique methods (in that order) to make // counts a sorted list of unique values cout << "Most frequent words:" << endl; // TODO: write a loop to process the five largest counts, allowing for the // case where there are fewer than five distinct counts. while ( false ) { // TODO: set highest to the largest item from the list // (it should be either the front or back) int highest = 0; // TODO: remove that item from the list by calling pop_front or pop_back // TODO: process all frequencies in order, checking for those with a count // matching highest and printing FIRST: SECOND on its own line // In this code, each element you process from frequencies will be // a pair and you get the left int by using .first and the // right by using .second } }