user64322 - 4 months ago 40

C Question

I was reading code for addition of sparse matrix and came across this condition

`if( sp1[0][0] != sp2[0][0] || sp1[0][1] != sp2[0][1] )`

{

printf("Invalid matrix size ");

exit(0);

}

Couldn't understand why the number of non-zero rows and columns should be same ?

Sorry I am a newbie.

The whole code is here

Answer

In matrix addition the number rows in the first matrix need to equal the number of rows in the second matrix.

The same is true about the columns.

The number of columns of the first matrix has to equal the number of columns of the second.

That is what that code you posted shows. Here is a quote from the linked code:

In this matrix sparse matrix is stored as follows

s[0][0]=Total rows in sparse matrix

s[0][1]=Total columns in sparse matrix

s[0][2]=Total number of non-zero values