Program in C for Sparse Matrix in Triplet Representation - CodingSoln


Sparse Matrix Representations


1. Write a program to represent an user defined sparse matrix in triplet representation


Program

#include <stdio.h>
#define MAX 20

void read_matrix(int a[10][10], int row, int column);
void print_sparse(int b[MAX][3]);
void create_sparse(int a[10][10], int row, int column, int b[MAX][3]);

int main()
{
    int a[10][10], b[MAX][3], row, column;
    printf("\nEnter the size of matrix (rows, columns): ");
    scanf("%d%d", &row, &column);

    read_matrix(a, row, column);
    create_sparse(a, row, column, b);
    print_sparse(b);
    return 0;
}

void read_matrix(int a[10][10], int row, int column)
{
    int i, j;
    printf("\nEnter elements of matrix\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }
}

void create_sparse(int a[10][10], int row, int column, int b[MAX][3])
{
    int i, j, k;
    k = 1;
    b[0][0] = row;
    b[0][1] = column;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (a[i][j] != 0)
            {
                b[k][0] = i;
                b[k][1] = j;
                b[k][2] = a[i][j];
                k++;
            }
        }
        b[0][2] = k - 1;
    }
}

void print_sparse(int b[MAX][3])
{
    int i, column;
    column = b[0][2];
    printf("\nSparse form - list of 3 triples\n\n");
    for (i = 0; i <= column; i++)
    {
        printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]);
    }
}


Output

Enter the size of matrix (rows, columns): 3 3

Enter elements of matrix
[0][0]: 1 3
[0][1]: [0][2]: 1 4 
[1][0]: [1][1]: 4 6 
[1][2]: [2][0]: 6 3
[2][1]: [2][2]: 3 7

Sparse form - list of 3 triples

3       3       9
0       0       1
0       1       3
0       2       1
1       0       4
1       1       4
1       2       6
2       0       6
2       1       3
2       2       3


2. Write a program in c to add two user defined sparse matrix and display the resultant triplet

Program


#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void printsparse(int b[MAX][3]);
void readsparse(int b[MAX][3]);

void addsparse(int b1[MAX][3],int
b2[MAX][3],int b3[MAX][3]);
void main()
{

	int b1[MAX][3],b2[MAX][3],b3[MAX][3];
	
	readsparse(b1);
	
	readsparse(b2);
	
	addsparse(b1,b2,b3);
	
	printsparse(b3);
}
void readsparse(int b[MAX][3])
{

	int i,t,m,n;
	
	printf("\nEnter no. of rows and columns:");
	
	scanf("%d%d",&m,&n);
	    printf("No. of non-zero triples:");
	
	scanf("%d",&t);
	
	b[0][0]=m;
	
	b[0][1]=n;
	
	b[0][2]=t;

	for(i=1;i<=t;i++)
	{
		printf("Enter the triples(row,column,value):");
		scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
	}
		printf("Another matrix:");
	}
		void addsparse(int b1[MAX][3],int
		b2[MAX][3],int b3[MAX][3])
	{

int t1,t2,i,j,k;

if(b1[0][0]!=b2[0][0]||b1[0][1]!=b2[0][1])
    {

printf("\nYou have entered invalid matrix!!Size must be equal");

exit(0);
    }

t1=b1[0][2];

t2=b2[0][2];

i=j=k=0;

b3[0][0]=b1[0][0];

b3[0][1]=b1[0][1];

while(i<=t1&&j<=t2)
    {

		if(b1[i][0]<b2[j][0])
//row numbers are not equal

			{
		
				b3[k][0]=b1[i][0];
				
				b3[k][1]=b1[i][1];
				
				b3[k][2]=b1[i][2];
				
				k++;
				
				i++;
		
			}

else if(b2[j][0]<b1[i][0])
//row numbers are not equal

	{

		b3[k][0]=b2[j][0];
		
		b3[k][1]=b2[j][1];
		
		b3[k][2]=b2[j][2];
		
		    k++;
		
		j++;

	}

else if(b1[i][1]<b2[j][1])
//row numbers are equal, compare column

	{

		b3[k][0]=b1[i][0];
		
		b3[k][1]=b1[i][1];
		
		b3[k][2]=b1[i][2];
		
		k++;
		
		i++;
    }

else if(b2[j][1]<b1[i][1])
//row numbers are equal, compare column

	{

		b3[k][0]=b2[j][0];
		
		b3[k][1]=b2[j][1];
		
		b3[k][2]=b2[j][2];
		
		k++;
		
		j++;

	}

else

	{

		b3[k][0]=b1[i][0];      //row and column numbers are equal
		
		b3[k][1]=b1[i][1];
		
		b3[k][2]=b1[i][2]+b2[j][2];
		
		k++;
		
		i++;
		
		j++;

	}
    }

while(i<=t1)        //copy remaining terms from b1
    {

		b3[k][0]=b1[i][0];
		
		b3[k][1]=b1[i][1];
		
		b3[k][2]=b1[i][2];
		
		i++;
		
		k++;
    }

while(j<=t2)        //copy remaining terms from b2
		{

		b3[k][0]=b2[j][0];
		
		b3[k][1]=b1[j][1];
		
		b3[k][2]=b1[j][2];
		
		j++;
		
		k++;
	}

b3[0][2]=k-1;       //set number of terms in b3
}
void printsparse(int b[MAX][3])
{

int i,t;

	t=b[0][2];
	
	printf("\nrow\tcolumn\tvalue");
	
	for(i=1;i<=t;i++)
	    {
	
			printf("\n%d\t%d\t%d",b[i][0],b[i][1],b[i][2]);
	    }
}


Output

Enter no. of rows and columns:3 4

No. of non-zero triples:2

Enter the triples(row,column,value):2 4 6

Enter the triples(row,column,value):5 3 7

Another matrix:

Enter no. of rows and columns:3 4

No. of non-zero triples:2

Enter the triples(row,column,value):4 7 9

Enter the triples(row,column,value):1 3 6

Another matrix:

row     column  value
2       4       6
4       7       9
1       3       6
5       3       7

Admin

Hi This is the Admin of CodingSoln. Currently Pursuing B. Tech Computer Science and Engineering form KIIT University India

Post a Comment

Previous Post Next Post