answersLogoWhite

0

// sparse matrix addition.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include<stdio.h>

#include<conio.h>

void read_sp_matrix(int sp[100][100]);

void add_sp_matrix( int sp1[100][100],int sp2[100][100],int sp3[100][100]);

void print_sp_matrix(int sp[100][100]);

int _tmain(int argc, _TCHAR* argv[])

{

int sp1[100][100],sp2[100][100],sp3[100][100],sp[100][100],i,j,k,r,c;

printf("Enter the first sparse matrix row size,column size & number of non zero elements");

read_sp_matrix(sp1);

printf("Enter the second sparse matrix row size,column size & number of non zero elements");

read_sp_matrix(sp2);

printf("You entired the first sparse matrix \n");

print_sp_matrix(sp1);

printf("You entired the second sparse matrix \n");

print_sp_matrix(sp2);

printf("Result of sparse matrix addition \n");

add_sp_matrix(sp1,sp2,sp3);

print_sp_matrix(sp3);

getch();

}

void read_sp_matrix(int sp[100][100])

{

int i,j,k,r,c,t;

scanf("d%d",&sp[0][0],&sp[0][1],&sp[0][2]);

r=sp[0][0];

c=sp[0][1];

k=1;

printf("Enter the sparse matrix elements \n");

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

{

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

{

scanf("%d",&t);

if(t != 0)

{

sp[k][0] = i;

sp[k][1] = j;

sp[k][2] = t;

k++;

}

}

}

}

void add_sp_matrix(int sp1[100][100],int sp2[100][100],int sp3[100][100])

{

int i,j,k,t1,t2,x;

t1 = sp1[0][2];

t2 = sp2[0][2];

i=j=k=1;

if((sp1[0][0] !=sp2[0][0])&&(sp1[0][1] !=sp2[0][1]))

{

printf("Invalid matrix ,try to correct \n");

goto x;

}

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

{

if(sp1[i][0] < sp2[j][0])

{

sp3[k][0] = sp1[i][0];

sp3[k][1] = sp1[i][1];

sp3[k][2] = sp1[i][2];

k++;

i++;

}

else if(sp1[i][0] > sp2[j][0])

{

sp3[k][0] = sp2[j][0];

sp3[k][1] = sp2[j][1];

sp3[k][2] = sp2[j][2];

k++;

j++;

}

if((sp1[i][0] sp2[0][1])

{

s = sp1[i][2] + sp2[j][2];

if(s != 0)

{

sp3[k][0] = sp1[i][0];

sp3[k][1] = sp1[i][1];

sp3[k][2] = sp1[i][2] + sp2[j][2];

k++;

i++;

j++;

x++;

}

else

{

i++;

j++;

}

}

else if(sp1[i][1] > sp2[j][1])

{

sp3[k][0] = sp2[j][0];

sp3[k][1] = sp2[j][1];

sp3[k][2] = sp2[j][2];

k++;

j++;

x++;

}

else

{

sp3[k][0] = sp1[i][0];

sp3[k][1] = sp1[i][1];

sp3[k][2] = sp1[i][2];

k++;

i++;

x++;

}

}

else if(sp1[i][0] > sp2[j][0])

{

sp3[k][0] = sp2[j][0];

sp3[k][1] = sp2[j][1];

sp3[k][2] = sp2[j][2];

k++;

j++;

x++;

}

else

{

sp3[k][0] = sp1[j][0];

sp3[k][1] = sp1[j][1];

sp3[k][2] = sp1[i][2];

k++;

i++;

x++;

}

}

while(i <= t1)

{

sp3[k][0] = sp1[i][0];

sp3[k][1] = sp1[i][1];

sp3[k][2] = sp1[i][2];

k++;

i++;

x++;

}

while(j <= t2)

{

sp3[k][0] = sp2[j][0];

sp3[k][1] = sp2[j][1];

sp3[k][2] = sp2[j][2];

k++;

j++;

x++;

}

sp3[0][0] = sp1[0][0];

sp3[0][1] = sp1[0][1];

sp3[0][2] = x/*sp1[0][2]*/;

int d = k-1;

printf("The Resultant Matrix \n");

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

{

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

{

printf("%2d ",sp3[i][j]);

}

printf("\n");

}

getch();

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
BeauBeau
You're doing better than you think!
Chat with Beau
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa

Add your answer:

Earn +20 pts
Q: Program for addition of two sparse matrix in 3 topule form?
Write your answer...
Submit
Still have questions?
magnify glass
imp