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

Sparse Add.c

The document discusses adding two sparse matrices. It prompts the user to enter the rows and columns of the matrices. It then prompts the user to enter the values for the first matrix P and second matrix Q. It prints out the sparse representations of P and Q with their non-zero elements. Finally, it calculates and prints the sum of the sparse matrices.

Uploaded by

Shuvam Pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Sparse Add.c

The document discusses adding two sparse matrices. It prompts the user to enter the rows and columns of the matrices. It then prompts the user to enter the values for the first matrix P and second matrix Q. It prints out the sparse representations of P and Q with their non-zero elements. Finally, it calculates and prints the sum of the sparse matrices.

Uploaded by

Shuvam Pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>

int main()

int p[20][20];

int q[20][20];

int r[20][20];

int i,j,row,col,count1=0,count2=0,count3=0;

printf("In order to add two sparse matrices the order of the two matrices must be equal,
since the order of the first martix is of 2x2 and the order of the second matrix must be 2x2\n");

printf("\n\nenter the rows and columns of matrix:\n");

scanf("%d %d",&row,&col);

printf("Enter the first matrix P:\n");

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

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

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

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

count1++;

printf("\n");

printf("Enter the second matrix Q:\n");

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

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

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

if(q[i][j]!=0)
{

count2++;

printf("\n");

printf("sparse matrix of P:\n");

printf("%d %d %d\n",row,col,count1);

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

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

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

printf("%d %d %d\n",(i+1),(j+1),p[i][j]);

printf("\n");

printf("sparse matrix of Q:\n");

printf("%d %d %d\n",row,col,count2);

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

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

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

printf("%d %d %d\n",(i+1),(j+1),q[i][j]);

printf("\n");
}

printf("sum of the sparse matrix is:\n");

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

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

if(p[i][j]!=0||q[i][j]!=0)

count3++;

r[i][j]=p[i][j]+q[i][j];

printf("%d %d %d\n",row,col,count3++);

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

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

if(p[i][j]!=0||q[i][j]!=0)

printf("%d %d %d\n",(i+1),(j+1),r[i][j]);

In order to add two sparse matrices the order of the two matrices must be equal, since the order of
the first martix is of 2x2 and the order of the second matrix must be 2x2

enter the rows and columns of matrix:

33
Enter the first matrix P:

120

000

345

Enter the second matrix Q:

100

240

501

sparse matrix of P:

335

111

122

313

324

335

sparse matrix of Q:

335

111

212

224

315

331

sum of the sparse matrix is:

337

112

122
212

224

318

324

336

--------------------------------

Process exited after 32.39 seconds with return value 0

Press any key to continue . . .

You might also like