-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransposition113.java
46 lines (39 loc) · 1.41 KB
/
Transposition113.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Write a code fragment to print the transposition (rows and columns changed)
* of a two-dimensional array with M rows and N columns.
*/
public class Transposition113 {
public static void printTranspose(int[][] array) {
int rows = array.length;
int columns = array[0].length;
// create new array to store the transposition
int[][] transposedArray = new int[columns][rows];
// fill the transposed array with values from the original array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transposedArray[j][i] = array[i][j];
}
}
// print the transposed array
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// create 3x4 two-dimensional array
int[][] exampleArray = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println("Original Array:");
for (int[] row :exampleArray){
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
// Print the transposed array
System.out.println("\nTransposed Array:");
printTranspose(exampleArray);
}
}