
consider a 2 dimensional array size m by n.Derive a function that can be used to reverse the elements of the array such that the last element of the array becomes the first..and the first element becomes the last.Code the algorithm and give the output.

Hello Friend,
Try the following code:
import java.util.*;
public class TDArray {
public static void reverse(int[][] arr){
int rows = arr.length;
int cols = arr[0].length;
int[][] reverse = new int[rows][cols];
for(int i = rows-1; i >= 0; i--) {
for(int j = cols-1; j >= 0; j--) {
reverse[rows-1-i][cols-1-j] = arr[i][j];
}
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(reverse[i][j]);
if(j < cols-1)
System.out.print(", ");
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
int rows, cols;
int[][] arr={{10,20,30,40,},{50,60,70,80}};
reverse(arr);
}
}
Thanks
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.