Why cant you just create a 2-dimensional array in function mode(), that contains the value in the original array and the count of the number of times it appears in the original array.
Code:
int mode (int arr[MAX])
{
int totals[2][MAX] = {0};
int i, nItemsAdded = 0;
for(i = 0; i < MAX; i++)
{
// search totals to see if the value is already there. If not, then
// add it. If its already in totals array then just increment
// the counter for it.
int j;
int found = 0;
for(j = 0; found == 0 && j < nItemsAdded; ++j)
{
if( totals[0][j] == arr[j])
{
found = 1; // set flag indicating value found
++totals[1][j]; // bump count
}
}
if( found == 0)
{
totals[0][nItemsAdded] = arr[i];
totals[1][nItemsAdded] = 1;
++nItemsAdded;
}
}
Bookmarks