You compare the third minimum against the values of temporary min1
and min2
. You must move the third loop after the outer for
loop
for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
if (m[i][j] < min1) {
min1 = m[i][j];
indexi_1 = i;
indexj_1 = j;
}
if (m[i][j] < min2 && indexi_1 != i && indexj_1 != j) {
min2 = m[i][j];
indexi_2 = i;
indexj_2 = j;
}
}
}
for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
if (m[i][j] < min3 && m[i][j] != m[indexi_1][indexj_1] && m[i][j] != m[indexi_2][indexj_2]) {
min3 = m[i][j];
indexi_3 = i;
indexj_3 = j;
}
}
}
Now the last loop will compare against the final first two minima.