0% found this document useful (0 votes)
24 views

Sparse Matrix DS LAB

The document contains C code to perform operations on sparse matrices including reading in elements, printing the matrices, calculating the transpose, sum and difference of the matrices.

Uploaded by

Akshat Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Sparse Matrix DS LAB

The document contains C code to perform operations on sparse matrices including reading in elements, printing the matrices, calculating the transpose, sum and difference of the matrices.

Uploaded by

Akshat Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>

#define MAX 20

int main()

int arr[4][5], array[4][5], sum[4][5], diff[4][5];

int i,j,cnt = 0,k=0;

printf("Enter the elements for the sparse matrix");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

scanf("%d",&arr[i][j]);

if(arr[i][j]!=0)cnt++;

printf("Enter the elements for the sparse matrix 2");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

scanf("%d",&array[i][j]);

printf("The output sparse array is:\n");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

{
printf("%d\t", arr[i][j]);

printf("\n");

int sparse[MAX][3];

sparse[0][0] = 4;sparse[0][1] = 5;sparse[0][2] = cnt;

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

if(arr[i][j]!=0)

sparse[i+1][0]=i;

sparse[i+1][1]=j;

sparse[i+1][2]=arr[i][j];

k++;

printf("The output array for sparse array 1 is:\n");

printf("rows\tcolumns\tvalues\n");

for(i = 0;i<k+1;i++)

for(j = 0;j<3;j++)

printf("%d\t", sparse[i][j]);
}

printf("\n");

printf("The transpose of sparse array 1 is:\n");

for(j = 0;j<5;j++)

for(i = 0;i<4;i++)

printf("%d\t", arr[i][j]);

printf("\n");

printf("The output sparse array 2 is:\n");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

printf("%d\t", array[i][j]);

printf("\n");

for(i = 0;i<4;i++)

{
for(j = 0;j<5;j++)

sum[i][j]= arr[i][j]+array[i][j];

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

diff[i][j]= arr[i][j]-array[i][j];

printf("The sum of sparse arrays is:\n");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

printf("%d\t", sum[i][j]);

printf("\n");

printf("The difference of sparse arrays is:\n");

for(i = 0;i<4;i++)

for(j = 0;j<5;j++)

printf("%d\t", diff[i][j]);
}

printf("\n");

You might also like