Java Problem: Implementing a method to print a cell's neighbors on a 2D array. The method's header is provided. The main method (do not alter it)w ill read N numbers to create the 2D array and it will call the method for you. Do not go out of the bounds of the 2D array. I have attached what it should look like with the following input: 4 4 1 2 3 4 7 8 9 10 13 14 15 16 19 20 21 22 2 2 Code I have so far: import java.util.Arrays; import java.util.Scanner; public class Problem3 { public static void printNeighbours(int[][] data, int row, int col){ } public static void main(String[] args) { Scanner kb = new Scanner(System.in); int[][] data = new int[kb.nextInt()][kb.nextInt()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) { data[i][j] = kb.nextInt(); } } System.out.println("The 2D Array:"); for (int[] cols : data) { System.out.println(Arrays.toString(cols)); } int row = kb.nextInt(); int col = kb.nextInt(); System.out.printf("The centre point is: %d %d (row,col)\n\n", row,col);
Java
Problem: Implementing a method to print a cell's neighbors on a 2D array. The method's header is provided. The main method (do not alter it)w ill read N numbers to create the 2D array and it will call the method for you. Do not go out of the bounds of the 2D array.
I have attached what it should look like with the following input:
4 4
1 2 3 4 7 8 9 10 13 14 15 16 19 20 21 22
2 2
Code I have so far:
import java.util.Arrays;
import java.util.Scanner;
public class Problem3 {
public static void printNeighbours(int[][] data, int row, int col){
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] data = new int[kb.nextInt()][kb.nextInt()];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = kb.nextInt();
}
}
System.out.println("The 2D Array:");
for (int[] cols : data) {
System.out.println(Arrays.toString(cols));
}
int row = kb.nextInt();
int col = kb.nextInt();
System.out.printf("The centre point is: %d %d (row,col)\n\n", row,col);
printNeighbours(data, row, col);
}
}
Step by step
Solved in 3 steps with 1 images