首页 > 解决方案 > How do I find the number that occurs the most in an array?

问题描述

const int NUM_MAX = 100;
const int HOWMANY = 30;

int main()
{
    vector<int> myVector;
    
    int counters[NUM_MAX] = { 0 };
    
    //to print out 15 numbers per line
    int x = 15;
    
    //seed random number generator
    srand(time(0));
    
    cout << "Array of 30 Random Numbers: "<< endl;
    
    for (int i = 0; i < HOWMANY; i++)
    {
        int myRandom = rand() % NUM_MAX;
        myVector.push_back(myRandom);
        
        int thisVal = myVector[i];
        cout << thisVal << " ";
        if (i % x == 0)
            cout << "\n";
    }
    
    cout << "     " << endl;
    
    return 0;
}

I'm just starting to learn about vectors and arrays, and my teacher has the counters array set up for us, but I'm not sure what to do with that. I have to find the number that occurs the most in the 30 random generated numbers, and I have to count how many times that number appeared. I'm very lost.

标签: c++

解决方案


我认为你让你自己的问题变得更难,通过让你的问题更容易思考,你可以更快地解决你的问题,正如我的朋友所说,在这个问题中,你可以将一个数组定义为一个计数器并计算其索引中的每个数字,比如在页面顶部说:)


推荐阅读