-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse.c
46 lines (45 loc) · 1.5 KB
/
sparse.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
#include <stdio.h>
int main()
{
int sparse[50][50], triplet[50][3], m, n, count = 0, temp = 1;
printf("Enter number of rows and columns:\n");
scanf("%d%d", &m, &n);
printf("Enter elements of sparse matrix:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &sparse[i][j]);
if (sparse[i][j] != 0) // condition for counting non zero elements
{
count++;
}
}
}
triplet[0][0] = m; // no of rows of sparse matrix
triplet[0][1] = n; // no of columns of sparse matrix
triplet[0][2] = count; // no.of non zero elements of sparse matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (sparse[i][j] != 0)
{ // this condition will filter out non zero elements
triplet[temp][0] = i; // row *index* of non zero element
triplet[temp][1] = j; // col *index* of non zero element
triplet[temp][2] = sparse[i][j]; // non zero element
temp++; // row increment
}
}
}
printf("triplet :\n");
for (int i = 0; i < count + 1; i++)
{ // total number of rows in triplet matrix will be number of non zero element +1;
for (int j = 0; j < 3; j++)
{
printf("%d ", triplet[i][j]);
}
printf("\n");
}
return 0;
}