---------------------------------- " Data Structure "-------------------------------------
ARRAY
---------------------------------- " Data Structure "-------------------------------------
ARRAYAddition of two Sparse Matrix
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
int t1,t2,i,j,k;
t1=b1[0][2];
t2=b2[0][2];
i=j=k=1;
b3[0][0]=b1[0][0];
b3[0][1]=b1[0][1];
while(i<=t1 && j<=t2)
{
/* If Both Rows And Columns Are Equal */
if(b1[i][0]==b2[j][0] && b1[i][1]==b2[j][1])
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2]+b2[j][2];
k++;
i++;
j++;
}
/* If Both Rows Are Equal But Column of first matrix is < Column of Second Matrix */
else if(b1[i][0]==b2[j][0] && b1[i][1]<b2[j][1])
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
/* If Both Rows Are Equal But Column of first matrix is > Column of Second Matrix */
else if(b1[i][0]==b2[j][0] && b1[i][1]>b2[j][1])
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
/* If Row of first matrix is > Row of Second Matrix */
else if(b1[i][0]>b2[j][0])
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
/* If Row of first matrix is < Row of Second Matrix */
else
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
}/*End Of While*/
while(i<=t1)
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
}
while(j<=t2)
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
j++;
k++;
}
b3[0][2]=k-1;
}/*End Of Function*/
This was the easiest explanation i could find!! Keep up the good work.
ReplyDelete