# calculating the mode

This is a discussion on calculating the mode within the C Programming forums, part of the General Programming Boards category; Hi there, So I have a 10 element array of int's and I need to calculate the mode. I haven't ...

1. ## 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?

2. 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.

3. 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?

4. 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.

5. You could sort the data, then loop through it looking for the longest string of repeat values.

6. Code:
```for (int i=0;i<10;i++)
{
//Do some stuff here
}```

7. 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);
}
}```

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

test_score isn't the same variable as test_scores.

9. > 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)

10. 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.

11. 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.