-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransposeMatrix.java
36 lines (33 loc) · 1.01 KB
/
TransposeMatrix.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
public class TransposeMatrix {
public static void transpose(int Matrix[][]) {
int m = Matrix.length;
int n = Matrix[0].length;
int transpose[][] = new int[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
transpose[j][i] = Matrix[i][j];
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int matrix[][] = { { 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 } };
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
System.out.println();
transpose(matrix);
}
}