1

I need to find the most occurring element in array C++. My code always returns 0.

p/s: This function should analyze the array to find the most occurring value in the array. You do not have to account for bi-modal occurrences, so the first modal value is enough to satisfy this function definition.

#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int* generateArray(int size);
int findMode(int* arr, int size);
default_random_engine eng(static_cast<unsigned>(time(0)));
uniform_int_distribution<int>randNum(0,20);

int main()
{
    // random 500 number for array coding

    int SIZE = 20;
    int* array = generateArray(SIZE); //output of 501 random element from 10-100
    cout << endl;
    findMode(array, SIZE);

    delete[] array; //delete new int [size]
    array = nullptr;
    return 0;
}
int* generateArray(int size)
{
    int* arr = new int[size];
    cout << "Random number of 501 elements: " << endl;
    cout << setw(6);
    for (int i = 0; i < size; i++) //loop
    {
        arr[size] = randNum(eng);
        cout << arr[size] << " ";
    }

    return arr; //return to array
}

int findMode(int* arr, int size)
{
    int max_count = 0;
    cout << "\nMost occurred number: ";
    for (int i = 0; i < size; i++) {
        int count = 1;
        for (int j = i + 1; j < size; j++)
            if (arr[i] == arr[j])
                count++;
        if (count > max_count)
            max_count = count;
        if (count == max_count)
            cout << arr[i] << endl;
    }
    return 0;
}
María Antignolo
  • 388
  • 4
  • 17
np1
  • 13
  • 4

3 Answers3

1

Modify the generateArray() and its calling like below and the for-loop should be changed.

void generateArray(int* arr, int size)
{

    cout << "Random number of 501 elements: " << endl;
    cout << setw(6);
    for (int i = 0; i < size; i++) //loop
    {
        arr[i] = randNum(eng);
        cout << arr[i] << " ";
    }
}

Above function modified for ease understanding. arr[size] th element is out of bound for array. You are setting value for same and trying to use it. That should be the index of array. Use index variable - i there as shown above.

int main()
{
    // random 500 number for array coding

    int SIZE = 20;
    int* arr = new int[SIZE];
    generateArray(arr, SIZE); //output of 501 random element from 10-100
    cout << endl;
    findMode(arr, SIZE);

    delete[] arr; //delete new int [size]
    arr = nullptr;
    return 0;
}
Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
  • Hi @Mannoj, the OP seems to be very grateful, did you call him? :) – anastaciu Feb 08 '20 at 18:04
  • No my friend, actually, I have posted 1st answer to question. Someone downvote it, So, I have deleted that old answer. – Build Succeeded Feb 08 '20 at 18:10
  • 1
    Oh, I see. UV for that... – anastaciu Feb 08 '20 at 18:12
  • @anastaciu actually there is nothing bad in DV but there should be helping comment to improve answer then it's more meaningful. – Build Succeeded Feb 08 '20 at 18:16
  • 1
    Well if the answer was correct the DV is bad, but yes, I agree nothing wrong with DV when applied correctly. – anastaciu Feb 08 '20 at 18:17
  • yes I got DV last time for just question I don't have answer and they told me I didn't make an effort to research( but i did a bunch of them). I don't see there is anything in your answer to DV at all .... – np1 Feb 08 '20 at 22:58
0

In the code snippet:

for (int i = 0; i < size; i++) //loop
{
    arr[size] = randNum(eng);
    cout << arr[size] << " ";
}

What you are doing is placing the generated number in arr[20] over and over again till the end of the cycle and printing it, witch besides leaving the array full of 'garbage' values is placing the generated values outside the array. arr[20] is actually position 21 but yours only has 20 (arr[0] to arr[19]).

So you should start by replacing that with:

for (int i = 0; i < size; i++) //loop
{
    arr[i] = randNum(eng);
    cout << arr[i] << " ";
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Thank you so much. You have saved my life. I had spent the whole day to try to figure out and only focus on the second function and not the first one. I appreciate a lot .(p/s: I am switching my career from medical to CS so have to learn coding from scratch. Thank you again for helping me and not considering my bad coding) – np1 Feb 08 '20 at 17:58
  • @ngocphan I'm not Mannoj, my friend, you still need to refactor the findMode function, it's not doing what it's suppose to do, best of luck in your carrer change. – anastaciu Feb 08 '20 at 18:01
  • One of the member downvote my answer to I have reposted with little modification. – Build Succeeded Feb 08 '20 at 18:06
  • @anastaciu: thank you for giving very detail explaination. Thank you again for helping me out. Yes I fixed it and it looks like working now:) – np1 Feb 08 '20 at 22:53
  • @ngocphan, you are welcome, don't forget to accept one of the answers. – anastaciu Feb 08 '20 at 22:56
  • @anastaciu: thanks i am new to this site and I was looking the way to endorse you guys but didn't see anything . If you don't say it i probably still don't know :)) – np1 Feb 08 '20 at 23:10
  • @ngocphan, yes it's a nice rep system, after you reach 15 rep you can also upvote questions and answers. You'll be gaining privileges as your rep grows. – anastaciu Feb 08 '20 at 23:18
  • @anastaciu: I see. I also upvote for you and others but I haven't reached 15 rep yet so it wasn't publicly displayed. – np1 Feb 08 '20 at 23:21
  • @ngocphan, you're almost there :) – anastaciu Feb 08 '20 at 23:24
0

Try this out. Make a variable for storing the most repeated number's index.

int findMode(int* arr, int size)
{
    int max_count = 0,maxval_index=0;
    cout << "\nMost occurred number: ";
    for (int i = 0; i < size; i++) {
        int count = 1;
        for (int j = i + 1; j < size; j++)
            if (arr[i] == arr[j])
                count++;
        if (count >= max_count){
            max_count = count;
            maxval_index=i;}
    }
    cout<<arr[maxval_index]<<endl;
    return 0;
}
Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33