-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse matrix
More file actions
55 lines (53 loc) · 772 Bytes
/
sparse matrix
File metadata and controls
55 lines (53 loc) · 772 Bytes
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
#include<stdio.h>
void main()
{
int a[10][10],b[10][3],m,n,s=1,i=0,j=0;
printf("Sparse matrix\n");
printf("Enter number of rows");
scanf("%d",&m);
printf("Enter number of columns\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter elements of row %d\n",i+1);
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Entered matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
b[0][0]=m;
b[0][1]=n;
s=1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!='\0')
{
b[s][0]=i;
b[s][1]=j;
b[s][2]=a[i][j];
s++;
}
}
}
b[0][2]=s-1;
printf("Sparse matrix representation \n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
}