# Statistical mode......

• 02-09-2002
Unregistered
Statistical 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
• 02-09-2002
golfinguy4
Not that I know of.
• 02-09-2002
tim545666
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.
• 02-10-2002
Unregistered
Thanks
I was wrestling around with an approach and you gave me one.

Thanks,

Dave
• 02-10-2002
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.

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; }```
That's tested and it works!!!
• 02-10-2002
Evil Robot
Quote:

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.

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; }```
That's tested and it works!!!

But what if there's more than one mode?
• 02-10-2002
samGwilliam
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:

Code:

```for (i = 0; i < 10;)   i += getFreq (num + i, &mostCommon, &frequency);```
And you could also declare frequency as a static variable inside getFreq instead.
• 02-10-2002
samGwilliam
Bloody smileys!!!
• 02-10-2002
Unregistered
Thanks
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