Well, you may be screwed by now, but the mode is simply the item in a list that occurs most frequently. For small ranges, I usually recommend a frequency table. Here's an example:
Code:
#include <stdio.h>
#include <stdlib.h>
int get_mode(int a[], int size, int max);
int
main(void)
{
int a[10];
int i;
for (i = 0; i < 10; i++) {
a[i] = rand() % 10;
}
printf("Mode: %d\n", get_mode(a, 10, 10));
return 0;
}
int
get_mode(
int a[], /* List of numbers */
int size, /* Size of list */
int max /* Upper range limit */
)
{
int *freq;
int i;
int mode;
/* Allocate and zero fill a table */
freq = calloc(max, sizeof *freq);
/* Increment the appropriate element for each occurance of a[i] */
for (i = 0; i < 10; i++) {
++freq[ a[i] ];
}
/* Find the first largest element */
mode = 0;
for (i = 0; i < max; i++) {
if (freq[i] > mode) {
mode = i;
}
}
free(freq);
return mode;
}
The idea is that the frequency table has an element for every possible value in a range. The list will only have values within that range, so if you use the item in the list as an index for the table, you can keep a high performance count of occurances. But don't forget that a list can have more than one mode. I'll leave the solution for that particular problem up to you.