-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
matrix_symmetric.c
68 lines (61 loc) · 1.7 KB
/
matrix_symmetric.c
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
/*
Matrix is a 2-D Array.
A Symmetric Matrix is a square matrix that is equal to its Transpose.
The code gives output as it checks whether the matrix is symmetric or not.
*/
#include<stdio.h>
#include<stdlib.h>
//Function to check whether the given matrix is Symmetric or not
void symmetric(int row,int column){
int **matrix,flag = 0;
matrix = (int **)malloc(row * sizeof(int *));
for(int i = 0; i < row; i++)
matrix[i] = (int *)malloc(column * sizeof(int));
//enter the value of matrices
printf("Enter matrix:\n");
for(int i = 0; i < row; i++)
for(int j = 0; j < column; j++)
scanf("%d",&matrix[i][j]);
for (int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
if (matrix[i][j] != matrix[j][i]){
flag = 1;
break;
}
}
if( flag == 1)
break;
}
if(flag == 1)
printf("The matrix isn't symmetric.\n");
else
printf("The matrix is symmetric.\n");
}
int main(){
int row,column;
printf("Enter number of rows of matrix:");
scanf("%d",&row);
printf("Enter number of coloumns of matrix:");
scanf("%d",&column);
//checking whether the given matrix is square matrix or not
if(row == column){
symmetric(row,column);
}
else
printf("The matrix is not square symmetric.\n");
return 0;
}
/*
Sample Output:
Enter number of rows of matrix:2
Enter number of coloumns of matrix:2
Enter matrix:
1 2
2 1
The matrix is symmetric.
Time Complexity : O(mn)
Space Complexity : O(mn)
where,
m = number of rows
n = number of columns
*/