Is there a quick and dirty way for determining the mode (most occuring number) of inputted numbers. Only need mode for one number even though its possible to have multiple numbers with the same frequency.
Thanks,
Dave
This is a discussion on Statistical mode...... within the C++ Programming forums, part of the General Programming Boards category; Is there a quick and dirty way for determining the mode (most occuring number) of inputted numbers. Only need mode ...
Is there a quick and dirty way for determining the mode (most occuring number) of inputted numbers. Only need mode for one number even though its possible to have multiple numbers with the same frequency.
Thanks,
Dave
Not that I know of.
Hehe, I just had to do that. It was a big pain in the ass, but I got it to work. I'm not going to do your homework for you (not just because it is homework, but I am lazy), but I will give you a hint. Make an array of numbers from 1 to a hundred, lets call it array. array[5] would equal how many times 5 occured. That should be enough info to get it to work.
I was wrestling around with an approach and you gave me one.
Thanks,
Dave
That's a silly idea. I thought of it, but what if you are using integers from 0-65535? A 128k array? Not a good idea. Here's my idea.
That's tested and it works!!!Code:#include <iostream.h> #include <conio.h> void sortNums (int *n); int getFreq (int *n, int *mostCommon, int *frequency); // Returns size of run-length found, regardless of whether it's the biggest. void main (void) { int i; int num [10]; int mostCommon; int frequency = 0; for (i = 0; i < 10; i++) { cout << "Num: "; cin >> num [i]; } sortNums (num); for (i = 0; i < 10; i++) i += (getFreq (num + i, &mostCommon, &frequency) - 1); cout << "Most common: " << mostCommon << endl; } void sortNums (int *n) { int i, j; int temp; for (i = 0; i < 9; i++) for (j = i; j < 10; j++) { if (n[j] > n[i]) { temp = n[i]; n[i] = n[j]; n[j] = temp; } } } int getFreq (int *n, int *mostCommon, int *frequency) { int f = 1; int startNum = *n; n++; // Pointer arithmetic. while (*n == startNum) // Find run-lengths. { f++; // We've found one more. n++; // Onto next element. } if (f > *frequency) // If more of this number than current most common: { *frequency = f; *mostCommon = startNum; } return f; }
MSVC++ 6.0
But what if there's more than one mode?Originally posted by samGwilliam
That's a silly idea. I thought of it, but what if you are using integers from 0-65535? A 128k array? Not a good idea. Here's my idea.
That's tested and it works!!!Code:#include <iostream.h> #include <conio.h> void sortNums (int *n); int getFreq (int *n, int *mostCommon, int *frequency); // Returns size of run-length found, regardless of whether it's the biggest. void main (void) { int i; int num [10]; int mostCommon; int frequency = 0; for (i = 0; i < 10; i++) { cout << "Num: "; cin >> num [i]; } sortNums (num); for (i = 0; i < 10; i++) i += (getFreq (num + i, &mostCommon, &frequency) - 1); cout << "Most common: " << mostCommon << endl; } void sortNums (int *n) { int i, j; int temp; for (i = 0; i < 9; i++) for (j = i; j < 10; j++) { if (n[j] > n[i]) { temp = n[i]; n[i] = n[j]; n[j] = temp; } } } int getFreq (int *n, int *mostCommon, int *frequency) { int f = 1; int startNum = *n; n++; // Pointer arithmetic. while (*n == startNum) // Find run-lengths. { f++; // We've found one more. n++; // Onto next element. } if (f > *frequency) // If more of this number than current most common: { *frequency = f; *mostCommon = startNum; } return f; }
Well, there's not a lot you can do about that is there? The guy said he only wanted one. The program will give the highest out of duplicate modes. To get the lowest, flip the > operator in the bubble-sort function.
I digress. Here are some optimisations:
And you could also declare frequency as a static variable inside getFreq instead.Code:for (i = 0; i < 10;) i += getFreq (num + i, &mostCommon, &frequency);
MSVC++ 6.0
Bloody smileys!!!
MSVC++ 6.0
Thanks,
Like you said his approach had no flexablity to it.
I figured for something like this someone already has a solution and no need to always reinventing the wheel.
I hear some companies keep routine things like this on file to save time when it comes to programming and not always reinvent the wheel when it has been invented.
thanks,
Dave