# Thread: need help to calculate mode of entries

1. ## need help to calculate mode of entries

i need some help to figure out how to find the most frequent entry that was inputed
Code:
```#include <stdio.h>
#include <math.h>
#define MAXARR 100
void main (void)
{
int
mode[MAXARR], i, entry;
int counter = 0;
int total = 0;
int total2 = 0;
int mostfreq = 0;
float average, stdev;
scanf("%d",&entry);
while (entry >= 0)
{
total = total + entry;
total2 = total2 + entry * entry;
mode[entry]++;
counter = counter +1;
scanf("%d",&entry);
}
average = (float)total / (float)counter;
for(i=0;i<100;i++)
{
if (mode[i] > mostfreq);
{
mostfreq = mode[i];
}
}
printf("Most common age is %d \n", i);
printf("It occurs %d times \n", mostfreq);
printf("There are %d ages recorded \n", counter);
stdev = sqrt (total2 / counter - average * average);
printf("The average age is : %6.2f \n", average);
printf("The standard deviation from the average is %6.2f \n", stdev);
getchar();
getchar();
}```

2. One idea would be to have two arrays, one for the numbers and one for the frequency of each number. As each number is entered you increment its frequency counter, when you're finished you simply look for the largest number in your array of frequency's and there you have it.

An advantage to doing it that way is that it gives you complete data so you could also display numbers in different age brackets or what ever.

Also, main should always return an int, it should never be void. But WD on the code tags, so many people miss those on their first post.

3. Another way to figure the most frequently entered age would be to have a nested loop. Something like:
Code:
```int ages[] = { 3, 3, 2, 7, 19, 4, 3, 8, 10, 7, 4, 5, 4, 17, 9, 4 };
int num_ages = 16;
int i, j, ctr, high_ctr = 0, high_index = 0;

for(i = 0;i < 16;++i)
{
ctr = 0;

for(j = i+1;j < 16;++j)
if(ages[i] == ages[j])
ctr++;

if(ctr > high_ctr)
{
high_ctr = ctr;
high_index = i;
}
}

printf("The age entered most frequently was: %d\n", ages[high_index]);```
Something like that