Recursive 2D Array Traversal
What is the output when the program is run?
public class TransposePrint {
public static void printTranspose(int[][] arr, int col, int row) {
if (col == arr[0].length) return;
if (row == arr.length) {
System.out.println();
printTranspose(arr, col + 1, 0);
return;
}
System.out.print(arr[row][col] + " ");
printTranspose(arr, col, row + 1);
}
public static void main(String[] args) {
int[][] mat = { {1, 2, 3}, {4, 5, 6} };
printTranspose(mat, 0, 0);
}
}
A
1 4 2 5 3 6
B
1 4
2 5
3 6
C
4 1
5 2
6 3
D
1 2 3 4 5 6
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE