2D Array Main Diagonal
Which line of code should replace the missing code to set the main diagonal elements to 1?
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][i] = 1;
B
matrix[i][0] = 1;
C
matrix[0][i] = 1;
D
matrix[i][matrix.length - 1 - i] = 1;
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE