Here are two C++ programs two add two sparse matrices
C++ program to add two sparse matrices
#include<iostream>
#include<iomanip>
using namespace std;
void show(int (&spare)[20][3])
{
cout<<"RowNO\tColNo\tValue";
for(int i=0;i<=spare[0][2];i++)
cout<<endl<<spare[i][0]<<setw(8)<<spare[i][1]<<setw(8)<<spare[i][2];
}
void inspare(int (&spare)[20][3])
{
cout<<endl<<"Enter number of rows and columns : ";
cin>>spare[0][0]>>spare[0][1];
cout<<"Enter number of non-zero terms in the matrix :";
cin>>spare[0][2];
for(int i=1;i<=spare[0][2];i++)
{
cout<<"Enter the row, column and value of non zero term ";
cin>>spare[i][0]>>spare[i][1]>>spare[i][2];
}
}
int addspare(int (&spare1)[20][3],int (&spare2)[20][3],int (&add)[20][3])
{
int i=1,j=1,k=1;
if(spare1[0][0]==spare2[0][0]&&spare1[0][1]==spare2[0][1])
{
add[0][0]=spare1[0][0];
add[0][1]=spare1[0][1];
while(i<=spare1[0][2])
{
if(spare1[i][0]==spare2[j][0]&&spare1[i][1]==spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2]+spare2[j][2];
i++,j++,k++;
}
else if(spare1[i][0]<spare2[j][0])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
else if(spare1[i][0]>spare2[j][0])
{
add[k][0]=spare2[j][0];
add[k][1]=spare2[j][1];
add[k][2]=spare2[j][2];
j++,k++;
}
else if(spare1[i][1]<spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
else if(spare1[i][1]>spare2[j][1])
{
add[k][0]=spare2[j][0];
add[k][1]=spare2[j][1];
add[k][2]=spare2[j][2];
j++,k++;
}
if(i>spare1[0][2])
{
while(j<=spare2[0][2])
{
add[k][0]=spare1[j][0];
add[k][1]=spare1[j][1];
add[k][2]=spare1[j][2];
j++,k++;
}
}
if(j>spare2[0][2])
{
while(i<=spare1[0][2])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
}
}
add[0][2]=k-1;
return 0;
}
return 1;
}
int main()
{
int spare1[20][3],add[20][3],spare2[20][3];
inspare(spare1);
show(spare1);
inspare(spare2);
show(spare2);
if(addspare(spare1,spare2,add))
cout<<"Matrix cannot be added ";
else
{
cout<<endl<<endl<<"Addition of matrix "<<endl;
show(add);
}
return 0;
}
#include<iomanip>
using namespace std;
void show(int (&spare)[20][3])
{
cout<<"RowNO\tColNo\tValue";
for(int i=0;i<=spare[0][2];i++)
cout<<endl<<spare[i][0]<<setw(8)<<spare[i][1]<<setw(8)<<spare[i][2];
}
void inspare(int (&spare)[20][3])
{
cout<<endl<<"Enter number of rows and columns : ";
cin>>spare[0][0]>>spare[0][1];
cout<<"Enter number of non-zero terms in the matrix :";
cin>>spare[0][2];
for(int i=1;i<=spare[0][2];i++)
{
cout<<"Enter the row, column and value of non zero term ";
cin>>spare[i][0]>>spare[i][1]>>spare[i][2];
}
}
int addspare(int (&spare1)[20][3],int (&spare2)[20][3],int (&add)[20][3])
{
int i=1,j=1,k=1;
if(spare1[0][0]==spare2[0][0]&&spare1[0][1]==spare2[0][1])
{
add[0][0]=spare1[0][0];
add[0][1]=spare1[0][1];
while(i<=spare1[0][2])
{
if(spare1[i][0]==spare2[j][0]&&spare1[i][1]==spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2]+spare2[j][2];
i++,j++,k++;
}
else if(spare1[i][0]<spare2[j][0])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
else if(spare1[i][0]>spare2[j][0])
{
add[k][0]=spare2[j][0];
add[k][1]=spare2[j][1];
add[k][2]=spare2[j][2];
j++,k++;
}
else if(spare1[i][1]<spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
else if(spare1[i][1]>spare2[j][1])
{
add[k][0]=spare2[j][0];
add[k][1]=spare2[j][1];
add[k][2]=spare2[j][2];
j++,k++;
}
if(i>spare1[0][2])
{
while(j<=spare2[0][2])
{
add[k][0]=spare1[j][0];
add[k][1]=spare1[j][1];
add[k][2]=spare1[j][2];
j++,k++;
}
}
if(j>spare2[0][2])
{
while(i<=spare1[0][2])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
i++,k++;
}
}
}
add[0][2]=k-1;
return 0;
}
return 1;
}
int main()
{
int spare1[20][3],add[20][3],spare2[20][3];
inspare(spare1);
show(spare1);
inspare(spare2);
show(spare2);
if(addspare(spare1,spare2,add))
cout<<"Matrix cannot be added ";
else
{
cout<<endl<<endl<<"Addition of matrix "<<endl;
show(add);
}
return 0;
}
Above program only works when input are given in increasing order of row and column for example if we give 1 1 5 before 0 0 6 than above program will not work .Therefore I have given one more program to solve this problem.
C++ program to add two spare matrices
#include<iostream>
#include<iomanip>
using namespace std;
void show(int (&spare)[20][3])
{
cout<<"RowNO\tColNo\tValue";
for(int i=0;i<=spare[0][2];i++)
cout<<endl<<spare[i][0]<<setw(8)<<spare[i][1]<<setw(8)<<spare[i][2];
}
void inspare(int (&spare)[20][3])
{
cout<<endl<<"Enter number of rows and columns : ";
cin>>spare[0][0]>>spare[0][1];
cout<<"Enter number of non-zero terms in the matrix :";
cin>>spare[0][2];
for(int i=1;i<=spare[0][2];i++)
{
cout<<"Enter the row, column and value of non zero term ";
cin>>spare[i][0]>>spare[i][1]>>spare[i][2];
}
}
int addspare(int (&spare1)[20][3],int (&spare2)[20][3],int (&add)[20][3])
{
int k=1,flag=0;
if(spare1[0][0]==spare2[0][0]&&spare1[0][1]==spare2[0][1])
{
add[0][0]=spare1[0][0];
add[0][1]=spare1[0][1];
for(int i=1;i<=spare1[0][2];i++,k++)
{
for(int j=1;j<=spare2[0][2];j++)
{
if(spare1[i][0]==spare2[j][0]&&spare1[i][1]==spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2]+spare2[j][2];
flag=1;
spare2[j][1]+=spare2[0][1];
break;
}
}
if(flag==0)
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
}
flag=0;
}
for(int i=1;i<=spare2[0][2];i++)
{
if(spare2[i][1]<spare2[0][1])
{
add[k][0]=spare2[i][0];
add[k][1]=spare2[i][1];
add[k][2]=spare2[i][2];
k++;
}
else
spare2[i][1]-=spare2[0][1];
}
add[0][2]=k-1;
return 0;
}
return 1;
}
int main()
{
int spare1[20][3],add[20][3],spare2[20][3];
inspare(spare1);
show(spare1);
inspare(spare2);
show(spare2);
if(addspare(spare1,spare2,add))
cout<<"Matrix cannot be added ";
else
{
cout<<endl<<endl<<"Addition of matrix "<<endl;
show(add);
}
return 0;
}
#include<iomanip>
using namespace std;
void show(int (&spare)[20][3])
{
cout<<"RowNO\tColNo\tValue";
for(int i=0;i<=spare[0][2];i++)
cout<<endl<<spare[i][0]<<setw(8)<<spare[i][1]<<setw(8)<<spare[i][2];
}
void inspare(int (&spare)[20][3])
{
cout<<endl<<"Enter number of rows and columns : ";
cin>>spare[0][0]>>spare[0][1];
cout<<"Enter number of non-zero terms in the matrix :";
cin>>spare[0][2];
for(int i=1;i<=spare[0][2];i++)
{
cout<<"Enter the row, column and value of non zero term ";
cin>>spare[i][0]>>spare[i][1]>>spare[i][2];
}
}
int addspare(int (&spare1)[20][3],int (&spare2)[20][3],int (&add)[20][3])
{
int k=1,flag=0;
if(spare1[0][0]==spare2[0][0]&&spare1[0][1]==spare2[0][1])
{
add[0][0]=spare1[0][0];
add[0][1]=spare1[0][1];
for(int i=1;i<=spare1[0][2];i++,k++)
{
for(int j=1;j<=spare2[0][2];j++)
{
if(spare1[i][0]==spare2[j][0]&&spare1[i][1]==spare2[j][1])
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2]+spare2[j][2];
flag=1;
spare2[j][1]+=spare2[0][1];
break;
}
}
if(flag==0)
{
add[k][0]=spare1[i][0];
add[k][1]=spare1[i][1];
add[k][2]=spare1[i][2];
}
flag=0;
}
for(int i=1;i<=spare2[0][2];i++)
{
if(spare2[i][1]<spare2[0][1])
{
add[k][0]=spare2[i][0];
add[k][1]=spare2[i][1];
add[k][2]=spare2[i][2];
k++;
}
else
spare2[i][1]-=spare2[0][1];
}
add[0][2]=k-1;
return 0;
}
return 1;
}
int main()
{
int spare1[20][3],add[20][3],spare2[20][3];
inspare(spare1);
show(spare1);
inspare(spare2);
show(spare2);
if(addspare(spare1,spare2,add))
cout<<"Matrix cannot be added ";
else
{
cout<<endl<<endl<<"Addition of matrix "<<endl;
show(add);
}
return 0;
}
No comments:
Post a Comment