My problem involves writing a function that takes an integer array and an index as arguements, and returns the value of the integer in the array that appears most frequently, only that if there are 2 or more values with the same frequency, the smallest should be returned.
I have been able to get this far:
Code:
void find_maxfreq(int A[], int index) {
int i=0, j=0, maxfreq=1, freq=1, number=0;
int numbers[MAXVALS];
for (i=0 ; i<index; i++) {
for (j=i+1; j<index; j++) {
if (A[i]==A[j]) {
freq++;
}
}
if (freq>=maxfreq) {
maxfreq = freq;
number = A[i];
freq = 1;
}
}
printf("Max freq : %d\n", maxfreq);
printf("Value : %d\n", number);
return;
}
this works great, but does not work for the last condition for the return of the smallest value when there are similar frequencies. I feel it may be neccessary to include another array to do this. Could anybody shed some light on this for me...