954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding the mode value and its frequency!

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?

notbunny
Newbie Poster
4 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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!

notbunny
Newbie Poster
4 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You