Sparse Add.c
Sparse Add.c
h>
int main()
int p[20][20];
int q[20][20];
int r[20][20];
int i,j,row,col,count1=0,count2=0,count3=0;
printf("In order to add two sparse matrices the order of the two matrices must be equal,
since the order of the first martix is of 2x2 and the order of the second matrix must be 2x2\n");
scanf("%d %d",&row,&col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&p[i][j]);
if(p[i][j]!=0)
count1++;
printf("\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&q[i][j]);
if(q[i][j]!=0)
{
count2++;
printf("\n");
printf("%d %d %d\n",row,col,count1);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(p[i][j]!=0)
printf("%d %d %d\n",(i+1),(j+1),p[i][j]);
printf("\n");
printf("%d %d %d\n",row,col,count2);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(q[i][j]!=0)
printf("%d %d %d\n",(i+1),(j+1),q[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(p[i][j]!=0||q[i][j]!=0)
count3++;
r[i][j]=p[i][j]+q[i][j];
printf("%d %d %d\n",row,col,count3++);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(p[i][j]!=0||q[i][j]!=0)
printf("%d %d %d\n",(i+1),(j+1),r[i][j]);
In order to add two sparse matrices the order of the two matrices must be equal, since the order of
the first martix is of 2x2 and the order of the second matrix must be 2x2
33
Enter the first matrix P:
120
000
345
100
240
501
sparse matrix of P:
335
111
122
313
324
335
sparse matrix of Q:
335
111
212
224
315
331
337
112
122
212
224
318
324
336
--------------------------------