# Thread: finding mode of an array

1. ## finding mode of an array

hi can someone please explain how to find the mode of an array. i have a program that takes input until EOF. then the program takes the inputs, finds the mean, median, mode, and standard deviation. i looked at past threads but i'm still confused on how to do it.
can someone explain how this works. you don't have to give me the code, as long as you can please explain how everything works. by the way, this program is due at 10 PM so i'm in desperate need. thanks

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