I'm trying to change the highest value of each row in a 2d array to 0. The problem I'm having is that it changes the highest value to zero, but it also changes the values following the highest value to zero. Here's my code:
public class Test2 {
public static double[][] array1 = //array to be used
{{7.51, 9.57, 6.28, 5.29, 8.7},
{8.07, 6.54, 5.44, 8.78, 8.66},
{9.34, 9.73, 7.19, 6.87, 6.48}};
public static void main(String[] args) {
double h = array1[0][0];// highest value
for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
h = array1[a][0];
for(int b = 0; b < array1[a].length; b++) {
if ( array1[a][b] > h){
array1[a][b] = 0;
h = array1[a][b];
}
}
}
System.out.println(); //print array with highest values in each row now changed to zero
for (int x = 0; x < array1.length; x++){
System.out.println();
for (int y = 0; y < array1[x].length; y++){
System.out.print(array1[x][y] + " ");
}
}
}
}
The current output is this:
7.51 0.0 0.0 0.0 0.0
8.07 6.54 5.44 0.0 0.0
9.34 0.0 0.0 0.0 0.0
While the desired output is this:
7.51 0.0 6.28 5.29 8.7
8.07 6.54 5.44 0.0 8.66
9.34 0.0 7.19 6.87 6.48
How can I make it change only the max value and not the others?