will find number which is used frequently(often) in x array

int i;
int x[i],frequency[i];

for(int i=0;i<3;i++)
{
cin>>x[i];
frequency[i]=0;
}

frequency[i]=0;

for(int i=0;i<3;i++)
{
frequency[i]++;
cout<<x[i];
} 
frequency[i]++;
cout<<x[i];
cout<<frequency[i];

what should I do extra

Dani AI

Generated

As already hinted, the immediate problems in the posted code are uninitialized/incorrect array sizing and reuse of the loop variable i outside its intended scope. Use a known size (or read n first) and a container like std::vector so you do not rely on undefined behavior. The goal is to return the value that appears most often (the mode); if multiple values tie, decide whether you want one winner (for example, the earliest seen) or all of them.

A simple, robust approach is: read n, read n integers into a std::vector<int>, count occurrences with std::unordered_map<int,int>, then scan the map to find the maximum count. The snippet below chooses the earliest input value when there is a tie.

#include <iostream>
#include <vector>
#include <unordered_map>
#include <limits>

int main() {
    int n;
    if (!(std::cin >> n)) return 0;
    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) std::cin >> a[i];

    std::unordered_map<int,int> freq, first_pos;
    for (int i = 0; i < n; ++i) {
        int v = a[i];
        if (!first_pos.count(v)) first_pos[v] = i;
        ++freq[v];
    }

    int mode = 0, bestCount = 0, earliest = std::numeric_limits<int>::max();
    for (const auto &p : freq) {
        int v = p.first, c = p.second;
        if (c > bestCount || (c == bestCount && first_pos[v] < earliest)) {
            bestCount = c; mode = v; earliest = first_pos[v];
        }
    }

    std::cout << mode << " occurs " << bestCount << " times\n";
}

Troubleshooting notes: do not declare arrays with an uninitialized variable as the size; initialize counters to zero; protect against empty input; and decide tie behavior explicitly. For the sample input given by (two equal numbers and one different), the code above will correctly report the repeated number and its count.

Recommended Answers

All 3 Replies

Scoping problems. Too many variables named i. Make sure i is what you think it is.

Line 2 - wrong. The numbers in the brackets is the size. It's not a variable, particularly not an uninitialized variable. I assume it should be 3 instead of i?

Line 10. Scoping problems. i is used uninitialiazed.

Same with lines 17 through 19.

What precisely are you trying to do?

there are array of numbers for example:
I typed: 3
I typed: 3
I typed: 5

and

frequently number is: 3

I want to write the code which will do this

there are array of numbers for example:
I typed: 3
I typed: 3
I typed: 5

and

frequently number is: 3

I want to write the code which will do this

You want to find the mode?

http://en.wikipedia.org/wiki/Mode_(statistics)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.