Codehs 8.1.5 Manipulating 2d Arrays High Quality
for (let r = 0; r < grid.length; r++) for (let c = 0; c < grid[r].length; c++) console.log(grid[r][c]);
// Task 3: Write a function that swaps the first and last row function swapFirstLastRow(matrix) // Your code here Codehs 8.1.5 Manipulating 2d Arrays
In the previous lessons, you learned how to create and access elements in 2D arrays (also known as matrices). In 8.1.5, you will go a step further: you will data inside 2D arrays. This is a critical skill for games (grids), data tables, image processing, and more. for (let r = 0; r < grid
Finally, mastering the manipulation of 2D arrays opens the door to advanced programming concepts. Once a student can confidently modify a grid, they possess the fundamental skills required for image processing (modifying pixel matrices), creating tile-based games (moving characters on a map), and solving algorithmic problems involving matrices, such as pathfinding or rotation. The ability to iterate, assess, and modify a specific cell in a grid is a staple of software engineering. Finally, mastering the manipulation of 2D arrays opens
Adding a new column to a 2D array requires modifying each row individually. You can use a loop to iterate over each row and add the new value.
public static int[][] rotate90(int[][] matrix) int n = matrix.length; int[][] rotated = new int[n][n]; for (int r = 0; r < n; r++) for (int c = 0; c < n; c++) rotated[c][n - 1 - r] = matrix[r][c];