1
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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Shazazer
  • 11
  • 2
  • 1
    You don't need a vector. You just need to increment `counters[myRandom]++` for each observed number in your first pass. Then add another for-loop that looks for the index in `counter` with the largest value. – selbie Apr 21 '21 at 00:57
  • Imagine you have an array of 6 numbers, 1,2,4,5,2,3. How might you 'manually' (without code) determine which number occurs most in the array? – 2785528 Apr 21 '21 at 01:29
  • [How to remove duplicate char in string in C](https://stackoverflow.com/a/63255183/3422102) (ignore the title, read about how to use a *Frequency Array* -- that's' what your teacher wants you to do with the `counters[]` array) You can do the same thing with [std::map](https://en.cppreference.com/w/cpp/container/map), but if you are just learning about vectors, the plain old frequency array will do. – David C. Rankin Apr 21 '21 at 01:59

2 Answers2

0

What you have described is finding the mode of the array.

As @selbie mentioned, You need to increment counters[myRandom]++ and find the maximum value of the counters array. The position of the max value is the mode(Max occuring number) and the value is the number of time it has appeared.

For Eg: Lets take the MAX_VALUE as 5 and HOWMANY(bad choice on variable name though :P) as 10

Let random generated array be

1 5 3 2 1 3 4 3 3 3

So at each loop iteration , you will be updating counter variable as counter[number]++. For Eg. in this case it will be

counter[0] = 0
counter[1] = 2
counter[2] = 1
counter[3] = 4
counter[4] = 1
counter[5] = 1

So, the maximum value of the array is at position 3 . So the mode( max occuring element ) is 3 and the number of times it appeared is 4.

NOTE IMHO, Your question states you are new to vectors and arrays. however as @selbie mentioned the question here is intended to check your lorgic on how will you compute mode rather than knowing the operations of a vector. programming at start may seem its more weighed towards fancy libraries and hard syntaxes, however its always the logical thinking ( algorithm ) that would be the backbone of all the coding problems.

ThivinAnandh
  • 286
  • 1
  • 8
  • Thank you very much, I'll take your advice and try to focus more on the logic behind the source code. – Shazazer Apr 21 '21 at 01:33
0

I think you make your problem harder for yourself, by making your problem easier and think easier you can solve your problems faster and as my friends said, in this problem you can define a array as a counter and count each number in its index, like said in the top of the page :)

Amir Abouei
  • 126
  • 8