Code:
#include<stdio.h>
#include<conio.h>
typedef struct sparse
{
int row;
int col;
int val;
}s;
s s1[10],s2[10],s3[20];
void read(int m1[][],int r1,int c1);
void display(s s1[],int m1[][],int r1,int c1);
void main()
{
int r1,c1,r2,c2,m1[4][4],m2[4][4],op=0;
printf("\nEnter the no. of rows and column in 1st matrix");
scanf("%d %d",&r1,&c1);
read(m1,r1,c1);
printf("\nEnter the no. of rows and columns in the 2nd matrix");
scanf("%d %d",&r2,&c2);
read(m2,r2,c2);
do
{
printf("\n\n1.Represent both in tuples form.");
printf("\n2.Addition of two sparse matrices");
printf("\n3.Simple transpose of matrices");
printf("\n4.Fast transpose of matrices");
printf("\nEnter your choice");
scanf("%d",&op);
switch(op)
{
case 1: printf("\n1st matrix is:: \n");
display(s1,m1,r1,c1);
printf("\n2nd matrix is:: \n");
display(s2,m2,r2,c2);
break;
case 2: printf("\nAddition of two sparse matrices is:: \n");
addition(s1,s2,s3);
break;
}
printf("\nDo you want to continue?y/n");
}while((getche()=='y')||(getche()=='Y'));
}
void read(int m1[4][4],int r1,int c1)
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
}
void display(s s1[10],int m1[4][4],int r1,int c1)
{
int i,j,k=1;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(m1[i][j]!=0)
{
s1[k].row=i;
s1[k].col=j;
s1[k].val=m1[i][j];
k++;
}
}
}
s1[0].row=r1;
s1[0].col=c1;
s1[0].val=k-1;
for(i=0;i<k;i++)
{
printf("\n\t%d\t %d\t %d\n",s1[i].row,s1[i].col,s1[i].val);
}
}