-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparsity.c
51 lines (44 loc) · 1.13 KB
/
sparsity.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
#include <stdio.h>
typedef struct
{
int row;
int col;
int value;
} spm;
int main()
{
spm sparse_matrix[100];
int m, n;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[20][20];
printf("Enter the elements of the matrix: ");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
int non_zero = 0;
sparse_matrix[0].row = m;
sparse_matrix[0].col = n;
int z = 1;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
if (matrix[i][j] != 0)
{
non_zero++;
sparse_matrix[z].row = i;
sparse_matrix[z].col = j;
sparse_matrix[z].value = matrix[i][j];
z++;
}
}
sparse_matrix[0].value = non_zero;
int i=0,j=0;
for(i=0;i<=non_zero;i++)
{
printf("%d %d %d ",sparse_matrix[i].row,sparse_matrix[i].col,sparse_matrix[i].value);
printf("\n");
}
float sparsity = (float)(non_zero)/(m*n);
printf("Sparsity = %0.3f\n", sparsity);
}