question archive Write program that has the class name Problem3 and that has the main method
Subject:Computer SciencePrice:8.89 Bought3
Write program that has the class name Problem3 and that has the main method. Leave the main method empty for now. • Write method named deepReverse that takes one parameter, a 2-dimensional (2D) integer array named arr and returns a new 2D integer array. • The method should create new array a such that rows and columns are the reverse of the array arr, such that first row of the array arr is the last row of the new array, second row is the second last row of the new array and so on. • Similarly, the first column in the array arr is the last column in the new array, second column is the second last column in the new array and so on. See sample usage below. • Create printArray method that takes a 2D integer array as a parameter and prints out the elements of each row on its own line separated by spaces. • Several sample usages are provided for you below. Use the sample usages in the main method to test your code (and use the printArray method to print out the results of calling the deepReverse method!).
Sample Method Usage Return Value int[][] arr1 = {{1, 2, 4, 0}, {3, 4, 5, 6}, 7, 8, 9, 12}};
int[][] a1 = deepReverse(arr1);
{{12, 9, 8, 7}, {6, 5, 4, 3}, {0, 4, 2, 1}};
int[][] arr2 = {{2, 8}, {7, 20}, {9, 3}, {5, 12}};
int[][] a2 = deepReverse(arr2);
{{12, 5}, {3, 9}, {20, 7}, {8, 2}};
The above question is solved using java language. All the methods are made static so that it can be called directly without creating the object of the class.
Step-by-step explanation
CODE:
public class Problem3
{
/*deepReverse is used to reveres the matrix
logic:- copy the matrix in a linear array and store it in the
resultant matrix in the reverse order of linear array*/
public static int[][] deepReverse(int arr[][]){
//n will have the number of rows in arr
//m will have number of columns in array
int n=arr.length;
int m=arr[0].length;
int counter=0;
//n*m is total number of element in the matrix
int[] linear_arr=new int[n*m];
//copy the elements in linear arrray
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
linear_arr[counter]=arr[i][j];
counter++;
}
}
int[][] res=new int[n][m];
counter--;
/*copy the values in reverse in resultant array
i.e start from counter index and decrease counter one
by one*/
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
res[i][j]=linear_arr[counter];
counter--;
}
}
//return the resultant array
return res;
}
//function to print the array
public static void printArray(int arr[][]){
int n=arr.length;
int m=arr[0].length;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] arr1 = {{1, 2, 4, 0}, {3, 4, 5, 6}, {7, 8, 9, 12}};
int[][] a1 = deepReverse(arr1);
System.out.println("1st matrix is :");
printArray(a1);
int[][] arr2 = {{2, 8}, {7, 20}, {9, 3}, {5, 12}};
int[][] a2 = deepReverse(arr2);
System.out.println("2nd matrix is :");
printArray(a2);
}
}
OUTPUT:
1st matrix is :
12 9 8 7
6 5 4 3
0 4 2 1
2nd matrix is :
12 5
3 9
20 7
8 2
please see the attached file.