-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathSymmetricMatrix.java
143 lines (107 loc) · 2.76 KB
/
SymmetricMatrix.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Java Program to check whether matrix is
// symmetric or not
// Importing all classes of
// java.util package
import java.util.*;
// Class
public class GFG {
// Matrix 1
// Method to check whether the matrix is
// symmetric or asymmetric
static void checkSymmetric(int mat[][], int row,
int col)
{
int i, j, flag = 1;
// Display message
System.out.println("The matrix formed is:");
// Nested for loop for matrix iteration
// Outer loop for rows
for (i = 0; i < row; i++) {
// Inner loop for columns
for (j = 0; j < col; j++) {
// Print matrix
System.out.print(mat[i][j] + "\t");
}
System.out.println("");
}
// Matrix 2
// Finding transpose of the matrix
int[][] transpose = new int[row][col];
// Again, nested for loop for matrix iteration
// Outer loop for rows
for (i = 0; i < row; i++) {
// Inner loop for columns
for (j = 0; j < col; j++) {
// Print matrix elements
transpose[j][i] = mat[i][j];
}
}
// Condition check over Matrix 1 with Matrix 2
if (row == col) {
// Outer loop for rows
for (i = 0; i < row; i++) {
// Inner loop for columns
for (j = 0; j < col; j++) {
// Comparing two matrices
if (mat[i][j] != transpose[i][j]) {
flag = 0;
break;
}
}
// Setting a flag value for symmetric matrix
if (flag == 0) {
// Display message
System.out.print(
"\nThe matrix is not symmetric");
break;
}
}
// Setting a flag value different from above
// for symmetric matrix
if (flag == 1) {
// Display message
System.out.print(
"\nThe matrix is symmetric");
}
}
// If it isn't a square matrix
// then it can't be a symmetric matrix
else {
// Display message
System.out.print(
"\nThe matrix is not symmetric");
}
}
// Main driver method
public static void main(String args[])
{
// Taking input from the user
Scanner sc = new Scanner(System.in);
// Declaring variables and setting flag to 1
int i, j, row, col, flag = 1;
// Taking input from the user
System.out.print("Enter the number of rows:");
row = sc.nextInt();
// Display message
System.out.print("Enter the number of columns:");
// Reading matrix elements individually using
// nextInt() method
col = sc.nextInt();
// Declaring a 2D array(matrix)
int[][] mat = new int[row][col];
// Display message
System.out.println("Enter the matrix elements:");
// Nested for loop for traversing matrix
// Outer loop for rows
for (i = 0; i < row; i++) {
// Inner loop for colummns
for (j = 0; j < col; j++) {
// Print matrix element
mat[i][j] = sc.nextInt();
}
}
// calling function made above to check
// whether matrix is symmetric or not
checkSymmetric(mat, row, col);
}
}