I'm working on a school assignment right now,
( populate an array of 50 elements with random integers ranging from 50-100,
find mean, mode, standard deviation and the number of elements that fall within the standard deviation)

I've written working code for everything except for the function that finds the mode (most recurring number). can you please point me in the right direction in finding the mode?

I've tried a number of ways, none of which seem to get me very far.

Sorry if I wasn't clear.

I'm looking to find the mode in an array populated with random numbers.

Mode meaning the most re-occurring number (in 1,2,2,3,4; 2 would be the mode)

How did you know 2 was the mode of (1,2,2,3,4)? Figure out how you knew that then you can tell the computer how to know it. Does that make sense?

I find that a piece of paper and a pencil help a lot. There are several ways I can think of to do this... the two simplest being rather brute-force but very intuitive. It is perfectly OK to add other variables to help keep track of what numbers you have looked at so far, how many times a particular number appears, etc.

Hope this helps.

P.S. What about (1,2,2,3,1)? Or (1,2,3)?

thanks duoas, heres my progress:

for(int a=0;a<50;a++)
    {
        for (int b=0;b<50;b++)
        {
            if (arrayA[a]==b)
                freq[b]++;
        }
    }

(arrayA[] is my array filled with random integers, and freq[] is my array that is supposed to find frequency)

this is what i've come up with so far, and I know it doesn't work.
My plan was to compare each element of the array to a number, (when it found a 3 in arrayA[], it would add 1 to freq[3], and so on) from there I would find the highest element in freq[], and display the element number as the mode.
well I guess I'll keep working at it, and again, any help is appreciated.

If mode is the most frequently occuring number then find the frequency of the first number in the array. Since it's the most frequent value you've found so far it must be the most frequent at the moment. Then find the frequency of the second number in the array and compare it to the most frequent you currently know about. If it's more frequent than the currently most frequent make it the most frequent. Keep going until you've looked at every value in the array. (There are ways to optimize this process but brute force isn't wrong. You can optimize later if you want, after you get the brute force part down.)

BTW your code is pretty well on the way to a possible solution. The outer loop will allow you to count the frequency of each char in the array. The inner loop will allow you to compare every value in the array with the current value. Right on.

Remember that a and b both are both indexes to elements in arrayA---you use a correctly, but not b. Fix it.

Add another variable to keep track of the most frequent variable and assign it a very low value before using it in the loops at all. Compare the frequency of the current value with the most frequent after you completed the counting of the current value and before you move on to the next value and you should be all set.

Thanks for all your help, I should be able to figure it out now, but I don't have time to work on it tonight. I should have the answer by tomorrow night however!

int b=0, counter=0, mode=1, largest=0;
    for(int i=0; i<50;i++)
    {
        for(int j=0; j<50;j++)
        {
            if(arrayA[i]==arrayA[j])
            {counter++;
            }
        }
        if (counter>largest)
        {
            largest=counter;
            mode=arrayA[i];
        }
        counter=0;
    }
    cout<<"The mode is: "<<mode<<endl;
}

ok heres what I came up with.
It's works, It finds the mode. Is there any way I can simplify it, and make it less brute force?

Not really. It is about as simple as it gets. You'd have to add a bit of complexity to make it less brute force, and for what you are doing it isn't worth the effort.

Good job, BTW.

commented: He was a great help in helping get my problem solved +1

Hey thanks for your assistance, this community is very helpful, I think I'll stick around :)

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.