943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1972
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 23rd, 2007
0

Re: Multimode//No mode?

>>However, if there is no mode (every number only appears once), it just returns the first number for mode. if there is more then one mode, it just returns the first mode.

What behavior do you want it to do ?

cout << "no mode" or "null" (doesnt really matter)

and if there is multiple modes, list them
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 24th, 2007
0

Re: Multimode//No mode?

what I would do is create another list of all the modes. The way you have it programmed it can print only one mode. Create a 2d array, the first dimension will have the actual value and the second dimension the number of times that value occurs in the original array. Something like this (untested)
  1. int array[2][length] = {0};
  2.  
  3. // populate array with values
  4. int nItemsInList = 1;
  5. array[0][0] = list[0];
  6. array[0][1] = 1;
  7. for(int i = 1; i < length; i++)
  8. {
  9. // find list[i] in array[0];
  10. int found = 0;
  11. for(int j = 0; j < nItemsInList; j++)
  12. {
  13. // search the array for list[i]. If found, then just increment
  14. // its count. If not found, then add it to the array.
  15. if( array[0][j] == list[i] )
  16. {
  17. array[1][j]++;
  18. found = 1;
  19. break;
  20. }
  21. }
  22. if( found == 0)
  23. {
  24. // not found in the array, so we must add it here.
  25. array[0][nItemsInList] = list[i];
  26. array[1][nItemsInList] = 1;
  27. nItemsInList++;
  28. }
  29. }

Now all that is left is to run through the array and find the items(s) with the largest quantity.
Last edited by Ancient Dragon; Apr 24th, 2007 at 12:08 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 24th, 2007
0

Re: Multimode//No mode?

would i need to declare any more variables? sorry, i kind of need things spelled out for me. thanks for the insight, ill test it soon
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Apr 24th, 2007
0

Re: Multimode//No mode?

see line 1
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 24th, 2007
0

Re: Multimode//No mode?

thanks... still triyng
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007

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: hex code
Next Thread in C++ Forum Timeline: Can't figure out whats wrong





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


Follow us on Twitter


© 2011 DaniWeb® LLC