Answer to Question #232579 in C for John

Question #232579

Write a program to represent a sparse matrix in three tuple format using linked list and write addition function to perform addition.


1
Expert's answer
2021-09-03T00:10:05-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

John
12.09.21, 11:26

Thanks a ton AssignmentExpert !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS