| | |
Multimode//No mode?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
>>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
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)
Now all that is left is to run through the array and find the items(s) with the largest quantity.
c Syntax (Toggle Plain Text)
int array[2][length] = {0}; // populate array with values int nItemsInList = 1; array[0][0] = list[0]; array[0][1] = 1; for(int i = 1; i < length; i++) { // find list[i] in array[0]; int found = 0; for(int j = 0; j < nItemsInList; j++) { // search the array for list[i]. If found, then just increment // its count. If not found, then add it to the array. if( array[0][j] == list[i] ) { array[1][j]++; found = 1; break; } } if( found == 0) { // not found in the array, so we must add it here. array[0][nItemsInList] = list[i]; array[1][nItemsInList] = 1; nItemsInList++; } }
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: hex code
- Next Thread: Can't figure out whats wrong
Views: 1628 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






