944,088 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7197
  • C++ RSS
Apr 24th, 2005
0

Finding the mode value and its frequency!

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notbunny is offline Offline
4 posts
since Apr 2005
Apr 24th, 2005
0

Re: Finding the mode value and its frequency!

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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 27th, 2005
0

Re: Finding the mode value and its frequency!

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

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
notbunny is offline Offline
4 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: AHHHH!! i cant figure this out.
Next Thread in C++ Forum Timeline: Selectoin SOrt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC