Sparse Matrix Addition(With out using pointers)


Sample o/p

/***************************************************

-> This programm is to implement sparse matrix
   addition , with out using pointers to store
   the matrix
-> This programm works for matrices which have
   less than 100 elements (U can just change)
-> This program works in microsoft vc++ 6.0 in
   windows xp
-> For simplicity it is assumed that there
   are no duplicates (U should not enter two
   elements with same row no. and column no. ie the
   the program can not detect the error)

****************************************************/

#include<iostream.h>
#define MAX 100

class sparse_matrix
{
private:
 int r1,c1,r2,c2;
 int n1,n2;         // no of elements in matrices
 int a1[MAX][3];
 int a2[MAX][3];
public:
 void read_matrix1();
 void read_matrix2();
 void display_matrix1();
 void display_matrix2();
 void add_matrices();
 void function();
};

void sparse_matrix::read_matrix1()
{
 cout<<“\nEnter values for matrix1 ::\n”;
 cout<<“Enter the no. of rows and columns ::”;
 cin>>r1>>c1;

 int m,n,d;
 int i=0;
 cout<<“\nEnter integer matrix elements for the matrix1::\n”;
 cout<<“\n(Enter -1 -1 for row no. or column no. to end)\n”;
 cout<<“Enter row no. , column no. ,and data ::”;
 
 cin>>m>>n>>d;

 while(m!=-1 && n!=-1 )
 {
  a1[i][0]=m;
  a1[i][1]=n;
  a1[i][2]=d;
  i++;
  cout<<“\nEnter row no. , column no. ,and data ::”;
  cin>>m>>n>>d;
 }
 n1=i;
 //step1 :: sorting by row no.

 int j,k;
 for(j=0;j<=n1-2;j++)
 {
  for(k=0;k<=n1-j-2;k++)
  {
   if(a1[k][0]>a1[k+1][0])
   {
    int t=a1[k][0];
    a1[k][0]=a1[k+1][0];
    a1[k+1][0]=t;

    t=a1[k][1];
    a1[k][1]=a1[k+1][1];
    a1[k+1][1]=t;

    t=a1[k][2];
    a1[k][2]=a1[k+1][2];
    a1[k+1][2]=t;
   }
  }
 }

 // step2 :: sorting by column no.
 for(j=0;j<=n1-2;j++)
 {
  for(k=0;k<=n1-j-2;k++)
  {
   if(a1[k][0]==a1[k+1][0] && a1[k][1]>a1[k+1][1])
   {
    int t;

    t=a1[k][1];
    a1[k][1]=a1[k+1][1];
    a1[k+1][1]=t;

    t=a1[k][2];
    a1[k][2]=a1[k+1][2];
    a1[k+1][2]=t;
   }
  }
 }

}

void sparse_matrix::read_matrix2()
{
 cout<<“\nEnter values for matrix2 ::\n”;
 cout<<“Enter the no. of rows and columns ::”;
 cin>>r2>>c2;

 int m,n,d;
 int i=0;
 cout<<“\nEnter integer matrix elements for the  matrix::\n”;
 cout<<“\n( Enter -1 -1 for row no. or column no. to end )\n”;
 cout<<“Enter row no. , column no. ,and data ::”;

 cin>>m>>n>>d;

 while(m!=-1 && n!=-1 )
 {
  a2[i][0]=m;
  a2[i][1]=n;
  a2[i][2]=d;
  i++;
  cout<<“Enter row no. , column no. ,and data ::\n”;
  cin>>m>>n>>d;
 }
  n2=i;

 // step1 :: sorting by row no.
 int j,k;
 for(j=0;j<=n2-2;j++)
 {
  for(k=0;k<=n2-j-2;k++)
  {
   if(a2[k][0]>a2[k+1][0])
   {
    int t=a2[k][0];
    a2[k][0]=a2[k+1][0];
    a2[k+1][0]=t;

    t=a2[k][1];
    a2[k][1]=a2[k+1][1];
    a2[k+1][1]=t;

    t=a2[k][2];
    a2[k][2]=a2[k+1][2];
    a2[k+1][2]=t;
   }
  }
 }

 // step2 :: sorting by column no.
 for(j=0;j<=n2-2;j++)
 {
  for(k=0;k<=n2-j-2;k++)
  {
   if(a2[k][0]==a2[k+1][0] && a2[k][1]>a2[k+1][1])
   {
    int t;
    t=a2[k][1];
    a2[k][1]=a2[k+1][1];
    a2[k+1][1]=t;

    t=a2[k][2];
    a2[k][2]=a2[k+1][2];
    a2[k+1][2]=t;
   }
  }
 }
}

void sparse_matrix::display_matrix1()
{
 for(int i=0;i<n1;i++)
  cout<<a1[i][0]<<‘\t'<<a1[i][1]<<‘\t’
   <<a1[i][2]<<endl;
}

void sparse_matrix::display_matrix2()
{
 for(int i=0;i<n2;i++)
  cout<<a2[i][0]<<‘\t'<<a2[i][1]<<‘\t’
   <<a2[i][2]<<endl;
}

void sparse_matrix::add_matrices()
{
 if(r1!=r2 || c1!=c2)
 {
  cout<<“addition is not possible”;
  return;
 }
 int i=0,j=0;
 cout<<“The elements in the resultant matrix are ::\n”;
 while(i<n1 && j<n2)
 {
  if(a1[i][0]==a2[j][0] &&
   a1[i][1]==a2[j][1])
  {
   cout<<a1[i][0]<<‘\t'<<a1[i][1]<<‘\t’
    <<a1[i][2]+a2[j][2]<<‘\n’;
   i++;j++;
  }
  else if(a1[i][0] == a2[j][0])
          {
   if(a1[i][1]<a2[j][1])
   {
   cout<<a1[i][0]<<‘\t'<<a1[i][1]<<‘\t’
    <<a1[i][2]<<‘\n’;
   i++;
   }
   else
   {
   cout<<a2[j][0]<<‘\t'<<a2[j][1]<<‘\t’
    <<a2[j][2]<<‘\n’;
   j++;
   }
  }
  else
  {
   if(a1[i][0] < a2[j][0])
   {
   cout<<a1[i][0]<<‘\t'<<a1[i][1]<<‘\t’
    <<a1[i][2]<<‘\n’;
   i++;
   }
   else
   {
   cout<<a2[j][0]<<‘\t'<<a2[j][1]<<‘\t’
    <<a2[j][2]<<‘\n’;
   j++;
   }
  }
 }
 while(i++<n1)
  cout<<a1[i][0]<<‘\t'<<a1[i][1]<<‘\t'<<a1[i][2]<<‘\n’;
 while(j++<n2)
  cout<<a2[j][0]<<‘\t'<<a2[j][1]<<‘\t'<<a2[j][2]<<‘\n’;
}

void sparse_matrix::function()

 read_matrix1();
 display_matrix1();
 read_matrix2();
 display_matrix2();
 add_matrices();
}

void main()
{
 sparse_matrix obj;
 obj.function();
}

Sample o/p

5 thoughts on “Sparse Matrix Addition(With out using pointers)”

  1. well mate thnks again bt to ask u for more help…..can u just make the same programme without using the class concept ….. i know how classes work bt still if u can

  2. There is nothing more to do , just remove the class and call all the functions from main.

    we just wanted to guide you in the way but not to take you to the destination.
    If we do your home work ,it is no more than spoiling you.

  3. hi sir this program is too good to the learners .
    and try to publish multiplication also.
    thankyou sir
    dinu,cse,svce,tpt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s