-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse_Matrix.c
More file actions
64 lines (53 loc) · 1.47 KB
/
Sparse_Matrix.c
File metadata and controls
64 lines (53 loc) · 1.47 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
#include <stdio.h>
#define mrow 20
#define mcolumn 20
#define srow 50
int main() {
int mat[mrow][mcolumn], sparse[srow][3];
int nzero = 0, mr, mc, i, j, s;
printf("Enter number of rows: ");
scanf("%d", &mr);
printf("Enter number of columns: ");
scanf("%d", &mc);
// Input the matrix elements
for(i = 0; i < mr; i++) {
for(j = 0; j < mc; j++) {
printf("Enter element for row %d, column %d: ", i + 1, j + 1);
scanf("%d", &mat[i][j]);
if(mat[i][j] != 0)
nzero++;
}
}
printf("Entered matrix is:\n");
for(i = 0; i < mr; i++) {
for(j = 0; j < mc; j++) {
printf("%5d", mat[i][j]);
}
printf("\n");
}
// First row of sparse matrix with meta-data
sparse[0][0] = mr;
sparse[0][1] = mc;
sparse[0][2] = nzero;
// Filling the sparse matrix with non-zero elements
s = 1;
for(i = 0; i < mr; i++) {
for(j = 0; j < mc; j++) {
if(mat[i][j] != 0) {
sparse[s][0] = i + 1;
sparse[s][1] = j + 1;
sparse[s][2] = mat[i][j];
s++;
}
}
}
// Printing the sparse matrix
printf("Sparse matrix is:\n");
for(i = 0; i <= nzero; i++) {
for(j = 0; j < 3; j++) {
printf("%5d", sparse[i][j]);
}
printf("\n");
}
return 0;
}