forked from XdithyX/Ds-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparsesymmetric.c
91 lines (77 loc) · 1.59 KB
/
sparsesymmetric.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
struct matrix{
int row;
int col;
int val;
};
void read(struct matrix m[]){
int n,col,row;
printf("Enter the no of rows and columns in the matrix: ");
scanf("%d%d",&row,&col);
m[0].row=row;
m[0].col=col;
int i,j,k=1,temp,count=0;
printf("Enter the Matrix elements:\n");
for(i=0;i<m[0].row;i++){
for(j=0; j<m[0].col;j++){
scanf("%d", &temp);
if(temp!=0){
m[k].row=i;
m[k].col=j;
m[k++].val=temp;
count+=1;
}
}
}
m[0].val=count;
}
void printMatrix(struct matrix m[]){
int i;
printf("\n\n\nrow\tcol\tval\n");
for(i=0;i<m[0].val+1;i++)
printf("%d\t%d\t%d\n",m[i].row,m[i].col,m[i].val);
printf("\n\n\n");
}
void transpose(struct matrix m[],struct matrix res[])
{ int i,j,k=1;
res[0].row=m[0].col;
res[0].col=m[0].row;
res[0].val=m[0].val;
for(i=1;i<m[0].col+1;i++)
for(j=1;j<m[0].val+1;j++)
{
if (m[j].col == i-1)
{
res[k].row = m[j].col;
res[k].col = m[j].row;
res[k].val= m[j].val;
k++;
}
}
}
int symmetry(struct matrix m[], struct matrix res[])
{
int flag =1,i;
if(m[0].val==res[0].val){
for(i=1;i<res[0].val+1;i++){
if(m[i].row!=res[i].row||m[i].col!=res[i].col||m[i].val!=res[i].val)
{ flag=0;
break;
}
}
}
else
flag=0;
return flag;
}
int main(){
struct matrix m[100],res[100];
read(m);
printMatrix(m);
transpose(m,res);
printMatrix(res);
if (symmetry(m,res))
printf("symmetric\n");
else
printf("not symmetric\n");
}