-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparsematrix.c
61 lines (58 loc) · 1.3 KB
/
sparsematrix.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
#include <stdio.h>
void read_matrix(int a[10][10],int row,int column);
void create_sparse(int a[10][10],int row,int column,int b[15][3]);
void print_sparse(int b[15][3]);
int main()
{
int a[10][10],b[15][3],row,column;
printf("\n enter the size of matrix(rows,columns) ");
scanf("%d%d",&row,&column);
read_matrix(a,row,column);
create_sparse(a,row,column,b);
print_sparse (b);
return 0;
}
void read_matrix(int a[10][10],int row,int column)
{
int i,j;
printf("\n enter elements of matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
}
void create_sparse(int a[10][10],int row,int column,int b[15][3])
{
int i,j,k;
k=1;
b[0][0]=row;
b[0][1]=column;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(a[i][j]!=0)
{
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}
}
}
b[0][2]=k-1;
}
void print_sparse(int b [15][3])
{
int i,column;
column=b[0][2];
printf("\n sparse form-list of 3 triplets\n");
for(i=0;i<=column;i++)
{
printf("%d\t %d\t %d\n",b[i][0],b[i][1],b[i][2]);
}
}