-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP33_MatrixTransposeAndSymmetry.java
More file actions
47 lines (41 loc) · 1.37 KB
/
Copy pathP33_MatrixTransposeAndSymmetry.java
File metadata and controls
47 lines (41 loc) · 1.37 KB
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
47
package programs;
/**
* ============================================================
* PROGRAM 33: Matrix Transpose and Symmetry Verification
* ============================================================
* Problem: WAP to:
* a) Find the Transpose of a matrix (swapping rows and columns)
* b) Check if a matrix is Symmetric (Matrix == Transpose)
* ============================================================
*/
public class P33_MatrixTransposeAndSymmetry {
public static int[][] transpose(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] trans = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
trans[j][i] = matrix[i][j];
}
}
return trans;
}
public static boolean isSymmetric(int[][] matrix) {
if (matrix.length != matrix[0].length) return false;
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (matrix[i][j] != matrix[j][i]) return false;
}
}
return true;
}
public static void main(String[] args) {
int[][] sym = {
{1, 2, 3},
{2, 4, 5},
{3, 5, 6}
};
System.out.println("Is matrix symmetric? " + isSymmetric(sym));
}
}