Saturday, April 12, 2014

Sparse matrix addition

program to implement sparse matrix addition using array



PROGRAM

#include<iostream.h>
using namespace std;
class sparse
{
      int a[20][20],b[20][20],k;
      public:
                  int m,n;
                  void read();
                  void display();
                  void convert();
                  void add(sparse,sparse);
                  void sparsedisp();
                  void original();
};
void sparse::read()
{
      int i,j;
      cout<<”Enter the order of matrix:”;
      cin>>m>>n;
      cout<<”Enter the elements:\n”;
      for(i=0;i<m;i++)
      {
      for(j=0;j<n;j++)
      {
      cin>>a[i][j];
      }
      }
}
void sparse::display()
{
      int i,j;
      for(i=0;i<m;i++)
      {
      for(j=0;j<n;j++)
      {
      cout<<a[i][j]<<”\t”;
      }
      cout<<”\n”;
      }
}
void sparse::convert()
{
      int i,j,k=1;
      for(i=0;i<m;i++)
      {
      for(j=0;j<n;j++)
      {
      if(a[i][j]!=0)
      {
      b[k][0]=i;
      b[k][1]=j;
      b[k][2]=a[i][j];
      k=k+1;
      }
      }
}
      k=k-1;
      b[0][0]=m;
      b[0][1]=n;
      b[0][2]=k;
}
void sparse::sparsedisp()
{
      int i,j;
      k=b[0][2];
      for(i=0;i<k+1;i++)
      {
      for(j=0;j<3;j++)
      {
      cout<<b[i][j]<<”\t”;
      }
      cout<<”\n”;
      }
}
void sparse::add(sparse s1,sparse s2)
{
      int i=1,j=1,k=1;
      while((i<=s1.k)&&(j<=s2.k))
      {
      if(s1.b[i][0] < s2.b[j][0])
      {
      b[k][0]=s1.b[i][0];
      b[k][1]=s1.b[i][1];
      b[k][2]=s1.b[i][2];
      i++;
      k++;
      }
      else if( s1.b[i][0] > s2.b[j][0])
      {
      b[k][0]=s2.b[j][0];
      b[k][1]=s2.b[j][1];
      b[k][2]=s2.b[j][2];
      j++;
      k++;
}
else
{
      if(s1.b[i][1] < s2.b[j][1])
      {
      b[k][0]=s1.b[i][0];
      b[k][1]=s1.b[i][1];
      b[k][2]=s1.b[i][2];
      i++;
      k++;
}
else if( s1.b[i][1] > s2.b[j][1])
{
      b[k][0]=s2.b[j][0];
      b[k][1]=s2.b[j][1];
      b[k][2]=s2.b[j][2];
      j++;
      k++;
}
else
{
      b[k][0]=s1.b[i][0];
      b[k][1]=s1.b[i][1];
      b[k][2]=s1.b[i][2]+s2.b[j][2];
      i++;
      k++;
      j++;
}
}
}
while(i<=s1.k)
{
      b[k][0]=s1.b[i][0];
      b[k][1]=s1.b[i][1];
      b[k][2]=s1.b[i][2];
      i++;
      k++;
}
while(j<=s2.k)
{
      b[k][0]=s2.b[j][0];
      b[k][1]=s2.b[j][1];
      b[k][2]=s2.b[j][2];
      j++;
      k++;
}
k=k-1;
b[0][0]=s1.m;
b[0][1]=s2.n;
b[0][2]=k;
m=s1.m;
n=s2.n;
}
void sparse::original()
{
      int i,j;
      for(i=0;i<m;i++)
      for(j=0;j<n;j++)
      a[i][j]=0;
      for(i=1;i<=k;i++)
      {
                  int r=b[i][0];
                  int c=b[i][[1];
                  a[r][c]=b[i][2];
      }
}
int main()
{
      sparse s1,s2,s3;
      s1.read();
      cout<<”Matrix is:\n”;
      s1.display();
      s1.convert();
      cout<<”The sparse matrix is:\n”;
      s1.sparsedisp();
      s2.read();
      cout<<”Matrix is:\n”;
      s2.display();
      s2.convert();
      cout<<”The sparse matrix is:\n”;
      s2.sparsedisp();
      s3.add(s1,s2);
      cout<<”\nThe resultant sparse matrix is:\n”;
      s3.sparsedisp();
      s3.original();
      cout<<”The resultant matrix is:\n”;
      s3.display();
      return 0;
}


OUTPUT

Enter the order of matrix:2
2
Enter the elements:
2
0
0
3
Matrix is:
2                0
0                3
The sparse matrix is:
2       2       2
0       0       2
1       1       3
Enter the  order of matrix:2
2
Enter the elements:
3
0
4
0
Matrix is:
3                0
4                0
The sparse matrix is:
2       2       3
0       0       3
1       0       4
The resultant sparse matrix is:
2       2       3
0       0       5
1       0       4
1       1       3
The resultant matrix is:
5                0
4                3

1 comments:

  1. Harrah's Casino - Dr. MD
    Caesars Entertainment 고양 출장안마 offers world-class gaming and luxurious hotel 강릉 출장안마 rooms 화성 출장안마 for all ages, with more than 500 slot machines, 인천광역 출장샵 a variety of table 대구광역 출장마사지 games,

    ReplyDelete