Home > Back-end >  how to array in c
how to array in c

Time:12-02

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<double> numbers(input_number);

    cout << "Enter numbers from 0 to 50: " << endl;
    for (int i = 0; i < input_number;   i) {
        cin >> numbers[i];
    }
    unordered_map<int, int> freq;
    for (int i = 0; i < numbers.size(); i  ) {
        freq[numbers[i]]  ;
    }

    for (int i = 0; i < numbers.size(); i  ) {

        cout << freq[numbers[i]];
    }

    return 0;
}

When the use inputs numbers, for example 1,1,1,2 the output should be "1" because it is the most frequent number but the output here became "3,3,3,1" How to solve this problem?enter image description here

CodePudding user response:

You are most of the way there. Your frequencies are all stored, and you just need to search through the unordered_map now to find the item that has the largest value. Since you're already using <algorithm> you can leverage std::max_element to help:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

int main()
{
    std::vector<int> numbers = { 1, 2, 3, 2, 3, 2, 2 };
    std::unordered_map<int, int> freq;
    for (int n : numbers)   freq[n];

    if (!freq.empty())
    {
        auto it = std::max_element(freq.begin(), freq.end(),
            [](const auto& a, const auto& b) { return a.second < b.second; });
        std::cout << "Most frequent is " << it->first << "\n";
    }
}

Output:

Most frequent is 2

Here, a custom comparison function is supplied that tests only the frequency part of the element (otherwise the default behavior will compare the full key/value pairs which will result in finding the largest number).

Note that because the map is unordered, there won't be a predictable outcome for tie-breakers (where more than one number is the most frequent). If you need to handle that in a more predictable way, you'll need to adjust the comparison function or possibly just loop over the container yourself if it requires additional work.

One option for tie-breaking is to choose the lowest number. That could be achieved by modifying the comparison to:

return std::make_pair(a.second, b.first) < std::make_pair(b.second, a.first);

CodePudding user response:

The problem is that you just output all the values in map. In a naive implementation you have to iterate through map and register the maximum value and it's frequency:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
    using std::cin, std::cout, std::endl;
    using std::vector, std::unordered_map;
    
    int input_number = 0, max_freq = 0, max_val = 0;
    cout << "How many numbers you want to input: " << endl;
    cin >> input_number;
    
    // Making input double may creates questions
    vector<int> numbers(input_number);

    cout << "Enter numbers from 0 to 50: " << endl;
    for (int i = 0; i < input_number;   i) {
        cin >> numbers[i];
    }
    
    unordered_map<int, int> freq;
    for (int i = 0; i < numbers.size(); i  ) {
        freq[numbers[i]]  ;
    }
    
    // iterating over the map and finding max value
    for (auto val : freq) {
        if( val.second > max_freq) {
            max_val = val.first;
            max_freq = val.second;
        }
    }
    cout << max_val;

    return 0;
}

Standard maps do store values as pairs of key and value (std::pair). This can be done in easier way: you can do that right while inputting the numbers.

for (int i = 0; i < numbers.size(); i  ) {
    int val = numbers[i];
    int curfreq =   freq[val];
    if (curfreq > max_freq) {
        max_val = val;
        max_freq = curfreq;
    }        
}

cout << max_val;

CodePudding user response:

Your array freq has to proper values in it. But your logic in the print out is wrong. You are printing the numbers twice. I guess you want to print all the numbers from 0 to 50.

cout << freq[i]

Then you see which entries have values or not. Then add some logic (which doesn't exist in your code) to pick the proper value. Like the biggest count..

  • Related