Write a program to add two sparse matrices and the resulting matrix should be converted to a representation matrix.
#include<stdio.h>
int main()
{
int matrix1[10][3],matrix2[10][3],matrix_sum[10][3], matrix_diff[10][3] ;
int m,n,p,q,t1,t2,s,d,element;
int i,j;
printf("Enter matrix rows and columns : ");
scanf("%d%d", &m, &n );
t1=t2=0;
printf("\nEnter element of the first matrix( %d * %d\n", m,n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &element);
if(element!=0)
{
t1=t1+1;
matrix1[t1][1]=i;
matrix1[t1][2]=j;
matrix1[t1][3]=element;
}
}
}
matrix1[0][1]=m;
matrix1[0][2]=n;
matrix1[0][3]=t1;
printf("\nEnter element of the second matrix( %d * %d\n", m,n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &element);
if(element!=0)
{
t2=t2+1;
matrix2[t2][1]=i;
matrix2[t2][2]=j;
matrix2[t2][3]=element;
}
}
}
matrix2[0][1]=m;
matrix2[0][2]=n;
matrix2[0][3]=t2;
printf("\n\nDisplaying the first matrix \n");
printf("\n\nrow\tcol\tvalue");
printf("\n-----------------------\n");
for(i=0;i<=t1;i++)
{
printf("%d\t%d\t%d\n", matrix1[i][1],matrix1[i][2],matrix1[i][3] );
}
printf("\n\nDisplaying the second matrix \n");
printf("\n\nrow\tcol\tvalue");
printf("\n-----------------------\n");
for(i=0;i<=t2;i++)
{
printf("%d\t%d\t%d\n", matrix2[i][1],matrix2[i][2],matrix2[i][3] );
}
i=j=s=d=1;
while((i<=t1)&&(j<=t2))
{
if(matrix1[i][1]==matrix2[j][1])
{
if(matrix1[i][2]==matrix2[j][2])
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix1[i][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix1[i][2];
matrix_sum[s][3]=matrix1[i][3]+matrix2[j][3];
matrix_diff[d][3]=matrix1[i][3]-matrix2[j][3];
i++;
j++;
if(matrix_sum[s][3]!=0)
s++;
if(matrix_diff[d][3]!=0)
d++;
}
else
{
if(matrix1[i][2]<matrix2[j][2])
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix1[i][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix1[i][2];
matrix_sum[s][3]=matrix_diff[d][3]=matrix1[i][3];
i++;
s++;
d++;
}
else
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix2[j][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix2[j][2];
matrix_sum[s][3]=matrix2[j][3];
matrix_diff[d][3]=0-matrix2[j][3];
j++;
d++;
s++;
}
}
}
else
{
if(matrix1[i][1]<matrix2[j][1])
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix1[i][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix1[i][2];
matrix_sum[s][3]=matrix_diff[d][3]=matrix1[i][3];
i++;
d++;
s++;
}
else
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix2[j][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix2[j][2];
matrix_sum[s][3]=matrix2[j][3];
matrix_diff[d][3]=0-matrix2[j][3];
j++;
s++;
d++;
}
}
}
if(i<=t1)
{
for(p=i;p<=t1;p++)
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix1[p][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix1[p][2];
matrix_sum[s][3]=matrix_diff[d][3]=matrix1[p][3];
s++;
d++;
}
}
else if(j<=t2)
{
for(p=j;p<=t2;p++)
{
matrix_sum[s][1]=matrix_diff[d][1]=matrix2[p][1];
matrix_sum[s][2]=matrix_diff[d][2]=matrix2[p][2];
matrix_sum[s][3]=matrix2[p][3];
matrix_diff[d][3]=0-matrix2[j][3];
s++;
d++;
}
}
matrix_sum[0][1]=matrix_diff[0][1]=m;
matrix_sum[0][2]=matrix_diff[0][2]=n;
matrix_sum[0][3]=s-1;
matrix_diff[0][3]=d-1;
printf("\n\nThe sum sparse matrix is :");
printf("\n\nrow\tcolumn\tElement");
printf("\n-----------------------\n");
for(i=0;i<s;i++)
{
printf("%d\t%d\t%d\n", matrix_sum[i][1],matrix_sum[i][2],matrix_sum[i][3] );
}
getch();
return 0;
}
Comments
Leave a comment