-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse_Matrix.c
More file actions
108 lines (90 loc) · 2.21 KB
/
Sparse_Matrix.c
File metadata and controls
108 lines (90 loc) · 2.21 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
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
#include <stdio.h>
void main() {
int A[100][100], B[100][100], i, m, n, j;
printf("Enter m:\n");
scanf("%d", &m);
printf("Enter n:\n");
scanf("%d", &n);
printf("Please enter the elements for %d x %d matrix below:\n",m,n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("The Matrix you entered above in row major form is :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d", A[i][j]);
printf("\t");
}
printf("\n");
}
printf("The Sparse of the Matrix is :\n");
B[0][0] = m;
B[0][1] = n;
int k = 1;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] != 0) {
B[k][0] = i + 1;
B[k][1] = j + 1;
B[k][2] = A[i][j];
k = k + 1;
}
}
}
k = k - 1;
B[0][2] = k;
for (i = 0; i <= k; i++) {
for (j = 0; j < 3; j++) {
printf("%d", B[i][j]);
printf("\t");
}
printf("\n");
}
int D[100][100]={0},x,y;
printf("Lets now make the transpose matrix from sparse matrix:\n");
printf("\n");
for (i = 1; i <= k; i++) {
for (j = 0; j < 3; j++) {
if(j==0)
y=B[i][j];
else if(j==1)
x=B[i][j];
}
D[(x-1)][(y-1)]=B[i][2];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d",D[i][j]);
printf("\t");
}
printf("\n");
}
int E[100][100];
printf("The compact form of the Transpose of the Matrix is:\n");
E[0][0] = m;
E[0][1] = n;
int U = 1;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (D[i][j] != 0) {
E[U][0] = i + 1;
E[U][1] = j + 1;
E[U][2] = D[i][j];
U = U + 1;
}
}
}
U = U - 1;
E[0][2] = U;
for (i = 0; i <= U; i++) {
for (j = 0; j < 3; j++) {
printf("%d", E[i][j]);
printf("\t");
}
printf("\n");
}
}