Finding the mode value and its frequency!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 4
Reputation: notbunny is an unknown quantity at this point 
Solved Threads: 0
notbunny notbunny is offline Offline
Newbie Poster

Finding the mode value and its frequency!

 
0
  #1
Apr 24th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Finding the mode value and its frequency!

 
0
  #2
Apr 24th, 2005
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:
  1. #include <cstddef>
  2. #include <iostream>
  3. #include <map>
  4.  
  5. #define length(x) (sizeof (x) / sizeof *(x))
  6.  
  7. int main()
  8. {
  9. int a[] = {1,5,4,5,8,9,5,1,4,7,8,5};
  10. std::map<int, int> freq;
  11.  
  12. for ( size_t i = 0; i < length ( a ); i++ )
  13. ++freq[a[i]];
  14.  
  15. std::map<int, int>::const_iterator it = freq.begin();
  16.  
  17. while ( it != freq.end() ) {
  18. std::cout<< it->first <<": "<< it->second <<std::endl;
  19. ++it;
  20. }
  21. }
Now it's just a matter of finding the item with the largest value for second.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 4
Reputation: notbunny is an unknown quantity at this point 
Solved Threads: 0
notbunny notbunny is offline Offline
Newbie Poster

Re: Finding the mode value and its frequency!

 
0
  #3
Apr 27th, 2005
thanks to give me an idea. I found out the different way to find the frequency. this is what I did :

  1. int j(1);
  2. mode[j] = slist[1];
  3. freq[j]= 1;
  4.  
  5. for (int i=2;i<=n;i++)
  6. {
  7. if (mode[j] == slist[i])
  8. freq[j]++;
  9. else
  10. {
  11. j++;
  12. mode [j] = slist [i];
  13. freq[j]= 1;
  14. }//end if
  15.  
  16. }//end for
<< moderator edit: added [code][/code] tags >>

well, I found the most frequency and then I found mode!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC