IN JAVA
Write a Java class Transpose.java that can create an 2-dimensional array, print it, and print it with the rows and columns transposed. The program should: createPatterned2DArray () Creates a two-dimensional array and returns it filled with whole numbers such that the elements have the following pattern: Row 1:10 + numRows*1 + 0, 10 + numRows*1 + 1, 10 + numRows*1 + 2,. .. Row 2:10 + numRows*2 + 0, 10 + numRows*2 + 1, 10 + numRows*2 + 2,. .. Row 3:10 + numRows*3 + 0, 10 + numRows*2 + 1, 10 + numRows*3 + 2,.. . print2DArray ():Prints the array by row where each element printed has a minimum width of 4 spaces and is left-justified Use system. out. printf () print2DArrayTransposed ():Prints the array transposing rows and columns maintaining the format where each element printed has a minimum width of 4 spaces and is left-justified. Use System.out.printf (). End each row with a newline and complete the output with a blank line. Sample Transpose output where input is 3 5
Expert Answer
public class Transpose {
static void createPatterned2DArray(int arr[][]) {
if (arr == null)
return;
for (int i = 0; i < arr.length; ++i) {
for (int j = 0; j < arr[i].length; ++j) {
arr[i][j] = 10 + arr.length * (i + 1) + j;
}
}
}
static void print2DArray(int arr[][]) {
if (arr == null)
return;
for (int i = 0; i < arr.length; ++i) {
System.out.println();
for (int j = 0; j < arr[i].length; ++j) {
System.out.print(arr[i][j] + ” “);
}
}
}
static void print2DArrayTransposed(int arr[][]) {
if (arr == null)
return;
System.out.println();
for (int j = 0; j < arr[0].length; ++j) {
System.out.println();
for (int i = 0; i < arr.length; ++i) {
System.out.print(arr[i][j] + ” “);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print(“Enter row and column: “);
int row = sc.nextInt();
int cols = sc.nextInt();
int arr[][] = new int[row][cols];
createPatterned2DArray(arr);
System.out.println(“Created Array”);
print2DArray(arr);
System.out.println(“nnTransposed Array”);
print2DArrayTransposed(arr);
}
}
============================================
See Output