I'm kind of lost. I'm trying to find the mode value in the list. All list are stroed in array. Anyone has any idea?

2
Contributors
2
Replies
5
Views
13 Years
Discussion Span
Last Post by notbunny

Well, do you understand what the frequency and mode are? The mode can be easily found once you've obtained the frequency, and the frequency is trivial with the standard library:

``````#include <cstddef>
#include <iostream>
#include <map>

#define length(x) (sizeof (x) / sizeof *(x))

int main()
{
int a[] = {1,5,4,5,8,9,5,1,4,7,8,5};
std::map<int, int> freq;

for ( size_t i = 0; i < length ( a ); i++ )
++freq[a[i]];

std::map<int, int>::const_iterator it = freq.begin();

while ( it != freq.end() ) {
std::cout<< it->first <<": "<< it->second <<std::endl;
++it;
}
}``````

Now it's just a matter of finding the item with the largest value for second.

thanks to give me an idea. I found out the different way to find the frequency. this is what I did :

``````int j(1);
mode[j] = slist[1];
freq[j]= 1;

for (int i=2;i<=n;i++)
{
if (mode[j] == slist[i])
freq[j]++;
else
{
j++;
mode [j] = slist [i];
freq[j]= 1;
}//end if

}//end for``````

<< moderator edit: added [code][/code] tags >>

well, I found the most frequency and then I found mode!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.