-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program8.c
64 lines (64 loc) · 1.27 KB
/
Program8.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
#include<stdio.h>
struct matrix{
int row;
int column;
int value;
};
int main()
{
int n,i,j,m,a[20][20];
struct matrix k[300];
printf("Enter the matrix\n");
printf("Enter the number of rows \n");
scanf("%d",&n);
printf("Enter the number of columns\n");
scanf("%d",&m);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
k[0].row=n;
k[0].column=m;
int c=1;
int p=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
k[c].row=i;
k[c].column=j;
k[c].value=a[i][j];
c++;
p++;
}
}
}
int l=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]==0)
{
l++;
}
}
}
k[0].value=p;
printf("\n\n");
printf("Row\tColumn\tValue\n");
for(i=0;i<=p;i++)
{
printf("%d\t%d\t%d\n",k[i].row,k[i].column,k[i].value);
}
float num,sparsity;
num=m*n;
sparsity=l/num;
printf("Sparsity of a matrix : %f",sparsity);
}