Write a program to represent a sparse matrix in three tuple format using linked list and write addition function to perform addition.
#include <stdio.h>
int main()
{
int s_Matrix[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
int s = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (s_Matrix[i][j] != 0)
s++;
int c_Matrix[3][s];
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (s_Matrix[i][j] != 0)
{
c_Matrix[0][k] = i;
c_Matrix[1][k] = j;
c_Matrix[2][k] = s_Matrix[i][j];
k++;
}
for (int i=0; i<3; i++)
{
for (int j=0; j<s; j++)
printf("%d ", c_Matrix[i][j]);
printf("\n");
}
return 0;
}
Comments
Thanks a ton AssignmentExpert !!
Leave a comment