2D Array Main Diagonal
The setDiagonal method below is intended to modify its 2D array parameter, matrix, by setting each element on the main diagonal to 1. Which line of code should replace /* missing code */ for the method to work as intended?
public class Diagonal {
public static void setDiagonal(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
/* missing code */
}
}
public static void main(String[] args) {
int[][] square = new int[3][3];
setDiagonal(square);
System.out.println(square[1][1]);
}
}
A
matrix[i][matrix.length - 1 - i] = 1;
B
matrix[i][i] = 1;
C
matrix[i][0] = 1;
D
matrix[0][i] = 1;
Question Leaderboard
Not enough data yet to show leaderboard.
