
Given any integer 2D array, design then implement a Java program that will add to each element in the array the corresponding column number and the corresponding row number. Then, it prints the array before and after modification. For example, if the program fed with following 2D array: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The output should be as follows:
original 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
new 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6

class TwoDArray{
public static void main(String[]args){
int[][] A = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
int[][] B = new int[3][5];
System.out.println("Original Array: ");
for(int i=0 ; i < A.length ; i++){
System.out.println();
for(int j=0 ; j < A[i].length ; j++){
System.out.print(A[i][j]+" ");
}
}
System.out.println();
System.out.println();
for (int i=0 ; i < B.length ; i++){
for (int j=0 ; j < B[i].length ; j++){
B[i][j] = i+j;
}
}
System.out.println("New Array: ");
for(int i=0 ; i < B.length ; i++){
System.out.println();
for(int j=0 ; j < B[i].length ; j++){
System.out.print(B[i][j]+" ");
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.