# calculating the mode

• 06-12-2006
bigggame
calculating the mode
Hi there,

So I have a 10 element array of int's and I need to calculate the mode.

I haven't written any code (for the calculation part) because I have no idea where to start. I was just hoping someone who has done this before or thinks they have a good idea could point me in the right direction, I would appreciate it.

Is there a function to do this for me in math.h?
• 06-12-2006
bivhitscar
The mode is just the average, should be pretty easy. I'd suggest trying it out first and then come here with a specific problem if you get stuck.
• 06-12-2006
bigggame
Hey, thanks i appreciate any help i can get..

I'm looking for:

"the value that appears most frequently in the values input by the user."

Is there a sorting function that checks for repeat input?
• 06-12-2006
bivhitscar
Oh crap, it is to, sorry about that. Still, you could at least write a loop or something, just to show us that you're trying. We like to help, but not without some effort from you first. :)
• 06-12-2006
jafet
You could sort the data, then loop through it looking for the longest string of repeat values.
• 06-12-2006
VirtualAce
Code:

```for (int i=0;i<10;i++) {   //Do some stuff here }```
• 06-12-2006
bigggame
ok, ok sorry guys don't hate me i was thinking of something like this (keep in mind I already ordered the entries from smallest to largest):

Code:

```void compute_mode(char test_scores[],int *n_scores) { int i, j,k; char mode[*n_scores]; int n_modes = 1; for(i=o;i<*n_scores;i++) {   for(j=i+1;j<*n_scores;j++)   {     if( test_scores[i] < test_scores[j] )       continue;     else       {       mode[n_modes] = test_scores[i];       n_modes++;       }     } } for(k = 0; k <n_modes; k++)   {   printf("Mode %s", mode);   } }```
• 06-12-2006
dwks
Code:

`printf("Mode %s", mode[]);`
That won't work.

test_score isn't the same variable as test_scores.
• 06-13-2006
Salem
> i was thinking of something like this
Close enough - now write something which actually compiles.

> char mode[*n_scores];
C doesn't (yet) support variable length arrays.

> for(i=o;i<*n_scores;i++)
I guess that o was meant to be 0 (zero)
• 06-13-2006
quzah
Quote:

Originally Posted by Salem
> char mode[*n_scores];
C doesn't (yet) support variable length arrays.

C99 does. Your compiler on the other hand...

Quzah.
• 06-13-2006
jafet
You can make it simpler. A whole lot simpler, in fact:

Let's say you want to find the mode of 0,0,1,1,1,2,4,5,5,5,5,6,7,7,8,10. Let's call a row of consecutive numbers (like 3,3,3) a "run". You can proceed thus:

We start with 0, because it is the first element. We find that the run of 0s is of length 2. This is the longest run so far.

We proceed to the next element, 1, which has a run of three elements. This is longer than the zero run, so we store this as the longest run instead. The length of the 0s run is forgotten, as it is not needed.

... ...

In the end, 5 is found to be the mode, as it has a run of 5. The trick is that finding the mode only requires that you keep track of the longest run SO FAR, while ignoring those runs shorter than the longest run found so far. After you've checked all the runs, the longest run so far is guaranteed to be your mode, because you've found the longest run in the whole array.

Tip: you will only need a loop counter, two variables to keep track of the longest run found, and two more to keep track of the current run. You will only need to run through the whole array once.