Recursive 2D Array Row Reversal
What is printed when the following program is executed?
public class ReverseRow {
public static void reverseRow(int[] row, int start, int end) {
if (start >= end) return;
int temp = row[start];
row[start] = row[end];
row[end] = temp;
reverseRow(row, start + 1, end - 1);
}
public static void reverseAll(int[][] arr, int i) {
if (i == arr.length) return;
reverseRow(arr[i], 0, arr[i].length - 1);
reverseAll(arr, i + 1);
}
public static void main(String[] args) {
int[][] grid = { {1, 2, 3}, {4, 5, 6} };
reverseAll(grid, 0);
for (int i = 0; i < grid.length; i++) {
for (int n : grid[i]) {
System.out.print(n + " ");
}
System.out.println();
}
}
}
A
1 2 3
6 5 4
B
3 2 1
4 5 6
C
3 2 1
6 5 4
D
1 2 3
4 5 6
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE