I know that my array has the correct information (i checked) but in my for loop it is coming up with the wrong values.
int mostFrequent = 0;
int secondMost = 0;
for(int i =0;i<256;i++){
if (counter[i] > mostFrequent) {
secondMost = mostFrequent;
mostFrequent = i
} else if (counter[i] < mostFrequent && counter[i] > secondMost){
secondMost = i;
}
}
I know that I am saving the integers mostFrequent and secondMost as the index of the array, that is on purpose. I am comparing the values of the array but I want to save the indexes of those values. The correct values should be 32 and 101 for the indexes but it is not coming out correctly.
>Solution :
You are comparing value with index!
Use counter[i] > counter[mostFrequent] instead of counter[i] > mostFrequent
int mostFrequent = 0;
int secondMost = 0;
for(int i =0;i<256;i++){
if (counter[i] > counter[mostFrequent]) {
secondMost = mostFrequent;
mostFrequent = i
} else if (counter[i] < counter[mostFrequent] && counter[i] > counter[secondMost]){
secondMost = i;
}
}