How would i go about finding the mode (the number that appears most frequently) in a list of numbers i put in an array? I'm in a comp. science class right now, and i just can't think of how i would do it.
How would i go about finding the mode (the number that appears most frequently) in a list of numbers i put in an array? I'm in a comp. science class right now, and i just can't think of how i would do it.
One way would be to loop through the numbers in the array and put them into a map<int,int> container where the key stores the number from the input array and the value represents the number of times that value has occured so far. Each time you add a key to the map, you increment its associated value. What results is essentially a sorted array (sorted by key) that stores the frequency, i.e. number of occurances, that a particular value has occured in the input array. You could then search through this map looking for the key that had the highest value.
Declaring the map variable and initializing it with the input array will take 2-3 lines of code. Performing the search and returning the key with the highest found value will take another half-dozen lines of code.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
You do realize there is a much easier way to solve this problem rather than using containers?
Mr. C: Author and Instructor
Yeah, you can just use pointers...I think. I wish I knew how.
You could also just use another array. but it could take up a bit more memory than needed. but for learning purposes, memory concerns are unnecessary at this point.
Just have an array the size of the highest possible numbers....and then for each cell in the array, just increment array2[array1[i]], then find the max of the 2nd array
hmm, now i have something to do..im gonna see if i can do this.
wouldn't sorting it be a good idea?
Last edited by the Wookie; 05-14-2003 at 04:55 PM.
alright, it uses 2 vectors, this was the best i can come up with for now, im kinda tired lol.
it just makes 2 parallel vectors, 1 holding the numbers, and one holding the counter. the main loop just searches the first list against itself, finding duplicates..
so if the list is
the left column being the list of numbers, the right column being the number of those numbers found..like it found two 5s and one 0 and one 1. then the last loop just searches the right column for the largest value of n, and in a case like this it shouldnt matter if it picks the first 5 or the second, since both of their right column counts will be the same. i hope that makes senseCode:5 2 0 1 1 1 5 2
i was also originally going to use arrays but i couldnt get them to cooperate, silly VC
edit: ignore the "bool found;" you can just delete that, i was trying out a different method before i came up with this one
Last edited by the Wookie; 05-14-2003 at 05:13 PM.
If any of you have the Deitel C++ How to Program text: Pages 278-282 has a program that finds the mean median and mode.
not that hard!Code:void mode(int freq[], int ans[], int size)
Mr. C: Author and Instructor
or.... he could just do a switch statement.....
if the mode is all your looking for....
you could make counters for each number.
and a switch something like this.
another way you could analize this... is with an adt.Code:for(int i = 0; i < numOfEntries; i++) { switch (array[i]) { case 1: 1ctr++; break; case 2: 1ctr++ break; } // etc. etc. just have them enter a fixed range so you arent killing yourslef over switch statments...
you could say.....
class operation
{
int virtual sortNum(int []) = 0;
}
thats a long way and the switch serves a WAY easier solution but if you wanna impress the teacher do one of those
What if you had a lot of numbers - you could have a very large switch statement. The loop will do it with short code.
Mr. C: Author and Instructor
This is what I was talking about:
Outputs: Mode is 4Code:#include <map> #include <iostream> using namespace std; int mode(int array[], int size) { // Create and load map container from input array map<int,int> IntMap; for( int a = 0; a < size; ++a ) IntMap[array[a]]++; // Find maximum value in map. map<int,int>::iterator it = IntMap.begin(); int max = it->first; for( ; it != IntMap.end(); ++it ) if( IntMap[it->first] > IntMap[max] ) max = it->first; return max; } int main() { int data[] = { 3, 4, 7, 2, 6, 9, 4, 8, 4 }; cout << "Mode is " << mode(data,sizeof(data)/sizeof(data[0])) << endl; return 0; }
The map serves the same function as the arrays and vector containers mentioned by the Wookie and Jamsan. What's not easy about that?
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Here's what i have so far. Please forgive any errors, unused methods, or just general idiocy. All i care about is functionality. After i get it to work, then i will go for finesse.
Sorry if the logic's hard to follow...i'm a total beginner! The mode is never right, and when i check the counter array, it has all these huge numbers instead of the actual amount of hits per number. Can anyone decipher my work and tell me what i did wrong?Code:#include <iostream.h> void getnumbers(int *array); void sort(int *array); void modemedian(int *array, int &mode, int &median); void print(int *array, int &mode, int &median); int main() { int array[21]; int mode, median; getnumbers(array); sort(array); modemedian(array, mode, median); print(array, mode, median); return 0; } void getnumbers(int *array) { int i; cout << "Enter 21 numbers. Hit enter for each one: "; for(i=0;i<21;i++) cin >> array[i]; } void sort(int *array) { int temp, i, j; for(int i=0;i<20;i++) { for (int j=i + 1;j<21;j++) { if (array[i] > array[j]) { temp = array[i]; array[i]=array[j]; array[j]=temp; } } } } void modemedian(int *array, int &mode, int &median) { int temp; int tempb = 0; int tempc = 0; int counters[21]; median = array[10]; //Mode section for(int i=0;i<21;i++) { for(int j=0;j<21;j++) { if (array[i]==array[j]) counters[i]++; } } cout << "Number of hits per number: "; //I put this here just so i could see what the counter array was getting for(int i=0;i<21;i++) cout << counters[i] << " "; cout << endl; for(int i=0;i<21;i++) { if (counters[i]>tempb) { tempb=counters[i]; tempc = i; } } mode=array[tempc]; //end mode section } void print(int *array, int &mode, int &median) { cout << "You typed: "; for(int i=0;i<21;i++) cout << array[i] << " "; cout<< endl << "Mode: " << mode; cout<< endl << "Median: " << median; }
The basic logic looks OK but I do see a potential issue with the counters array not being initialized.
Other than that, it looks like it should work.Code:void modemedian(int *array, int &mode, int &median) { int temp; int tempb = 0; int tempc = 0; // Extra variable, not needed, see below int counters[21]; median = array[10]; //Mode section for(int i=0;i<21;i++) { counters[i] = 0; // Make sure you initialize the counter // before attemtping to increment unknown values for(int j=0;j<21;j++) { if (array[i]==array[j]) counters[i]++; } } cout << "Number of hits per number: "; //I put this here just so i could see what the counter array was getting //My compiler would have "issues" with using the same variable "i" // at this spot so I changed it for(int k=0;k<21;k++) cout << counters[k] << " "; cout << endl; // Again, use of "i" variable here would cause me "issues" // with my compiler so I changed it. for(int l=0;l<21;l++) { if (counters[l]>tempb) { tempb=counters[l]; //tempc = i; // Not needed... mode = array[l]; // Use this instead } } //mode=array[tempc]; // Not needed, replaced above //end mode section }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
This seems like the easiest solution:
Code:int mode(int* array, int size) { int largestNum = 0, largestCount = 0; int* upperLimit = std::max_element(&array[0], &array[size]); int* lowerLimit = std::min_element(&array[0], &array[size]); for (int i = *lowerLimit; i <= *upperLimit; ++i) { int currentCount = std::count(&array[0], &array[size], i); if (currentCount > largestCount) { largestCount = currentCount; largestNum = i; } } return largestNum; }
Last edited by Eibro; 05-15-2003 at 03:08 PM.