Friday, 7 December 2012

Sparse matrix cpp program

Program to implement various operations on sparse matrix 

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class sm
{ private:
          int rows,cols;
          int terms;
          struct term
          { int row,col,value;}*arr;
 public:
          void smread();
          void smdisplay();
          void smtranspose(sm &);
          void smadd(sm,sm);
          void smsubstract(sm,sm);
          int getrows()
          { return(rows); }
          int getcols()
          { return(cols);}
};
void sm::smread()
{        int k,f;
cout<<"Enter the matrix characteristics\n";
cout<<"Number of rows : ";

cin>>rows;
cout<<"Number of columns : ";
cin>>cols;
cout<<"Number of non zero elements : ";
cin>>terms;
arr=new term[sizeof(terms)*terms];
cout<<"\n Enter the non zero terms of the matrix ";
cout<<"\n Non zero element can be entered in any order\n\n";
for(int i=0;i<terms;i++)
{        cout<<"Enter the details of the non zero element "<<i+1<<" : ";
                   do
                   {        f=0;
                             do
                             {        cout<<"Row(begin with zero) : ";
                                      cin>>arr[i].row;
                                      if((arr[i].row<0)||(arr[i].row>rows-1))
                                      cout<<"Invalid row.Please re-enter\n";
                             }
                             while((arr[i]. row<0)||(arr[i].row>rows-1));
                             do
                             {        cout<<"column(begin with zero) : ";
                                      cin>>arr[i]. col;
                                      if((arr[i].col<0)||(arr[i]. col>cols-1))
                                      cout<<"Invalid column.Please re-enter\n";
                             }
                             while((arr[i]. col<0)||(arr[i]. col>cols-1));
                             for(k=0;k<i;k++)
                             if((arr[k].row==arr[i].row)&&( arr[k].col==arr[i].col))
{        cout<<"The non zero element ("<<arr[i].row<<","<<arr[i].col<<") alreay exist.Reenter element" <<i+1<<"\n";
                                      f=1;
                                      break;
                             }
                   }
while(f);
do
{        cout<<"Value           : ";
cin>>arr[i].value;
if(arr[i].value==0)
                   cout<<"Value cannot be zero .Please reenter\n";
          }
          while(arr[i].value==0);
          cout<<"\n";
}
return;
}
void sm::smdisplay()
{        cout<<"Matrix characteristics : ";
cout<<"rows= "<<rows<<", columns = "<<cols<<" and         Non-zero elements=" <<terms<<"\n";
cout<<"The sparse matrix is\n";
for(int i=0;i<rows;i++)
{        for(int j=0;j<cols;j++)
                   {        int flag=0;
                             for(int k=0;k<terms;k++)
                             if((arr[k].row==i)&&(arr[k].col==j))
                             {        cout<<arr[k].value<<" ";
                                      flag=1;
                                      break;
                             }
                             if(!flag) cout<<0<<" " ;
}
cout<<"\n";
}
return;
}
void sm::smtranspose(sm &m)
{        m.rows=cols;
          m.cols=rows;
          m.terms=terms;
m.arr=new term[terms];
for(int i=0;i<terms;i++)
{        m.arr[i].row=arr[i].col;
                   m.arr[i].col=arr[i].row;
                   m.arr[i].value=arr[i].value;
          }
return;
}
void sm::smadd(sm m1,sm m2)
{        rows=m1.rows;
cols=m1.cols;
terms=m1.terms+m2.terms;
arr=new term[terms];
int n=0;
for(int i=0;i<m1.rows;i++)
                   for(int j=0;j<m1.cols;j++)
                   {        int flag1=0;
                             int flag2=0;
                             for(int k=0;k<m1.terms;k++)
                             if((i==m1.arr[k].row)&&(j==m1.arr[k].col))
                             {        flag1=1; break;}
                             for(int l=0;l<m2.terms;l++)
                                      if((i==m2.arr[l].row)&&(j==m2.arr[l].col))
                                      {        flag2=1; break;}
                             if((flag1==1)&&(flag2==1))
                             {        if((m1.arr[k].value+m2.arr[l].value)!=0)
                                      {        arr[n].row=m1.arr[k].row;
                                                arr[n].col=m1.arr[k].col;
arr[n++].value=m1.arr[k].value + m2.arr[l].value;
                                      }
                             }
                             else
                                      if(flag1==1)
                                      {        arr[n].row=m1.arr[k].row;
                                                arr[n].col=m1.arr[k].col;
                                                arr[n++].value=m1.arr[k].value;
                                      }
                                      else
                                                if(flag2==1)
                                                {        arr[n].row=m2.arr[l].row;
                                                          arr[n].col=m2.arr[l].col;
                                                          arr[n++].value=m2.arr[l].value;
                                                }
                   }
                   terms=n;
                   return;
}
void sm::smsubstract(sm m1,sm m2)
{        rows=m1.rows;
cols=m1.cols;
terms=m1.terms+m2.terms;
arr=new term[terms];
int n=0;
for(int i=0;i<m1.rows;i++)
                   for(int j=0;j<m1.cols;j++)
                   {        int flag1=0;
                             int flag2=0;
                             for(int k=0;k<m1.terms;k++)
                             if((i==m1.arr[k].row)&&(j==m1.arr[k].col))
                             {        flag1=1; break;}
                             for(int l=0;l<m2.terms;l++)
                             if((i==m2.arr[l].row)&&(j==m2.arr[l].col))
                             {        flag2=1; break;}
                             if((flag1==1)&&(flag2==1))
                             {        if((m1.arr[k].value-m2.arr[l].value)!=0)
                                      {        arr[n].row=m1.arr[k].row;
                                                arr[n].col=m1.arr[k].col;
arr[n++].value=m1.arr[k].value - m2.arr[l].value;
                                      }
                             }
                   else
                             if(flag1==1)
                             {        arr[n].row=m1.arr[k].row;
                                      arr[n].col=m1.arr[k].col;
                                      arr[n++].value=m1.arr[k].value;
                             }
                             else
                                      if(flag2==1)
                             {        arr[n].row=m2.arr[l].row;
                                      arr[n].col=m2.arr[l].col;
                                      arr[n++].value=m2.arr[l].value;
                             }
          }       
          terms=n;
          return;
}
void main()
{        sm m1,m2,m3;   int c;
while(1)
          {        clrscr();
                   cout<<"\nPerform the following matrix operations";
                   cout<<"\n-------------------------------";
                   cout<<"\n1.Input Matrices";
                   cout<<"\n2.Display matrices";
                   cout<<"\n3.Display transpose of the matrices";
                   cout<<"\n4.Add matrices";
                   cout<<"\n5.Substract matrices";
                   cout<<"\n6.Exit from the program";
                   do
                    {        cout<<"\nSelect your choice(1:6)  ";
cin>>c;
                             cout<<"";
                    }
                   while((c<1)||(c>6));
          if(c==6)exit(1);
          clrscr();
          switch(c)
{ case 1 :
          clrscr();
                   cout<<"\nFirst matrix: \n";
                   m1.smread();
                   cout<<"\nsecond matrix: \n";
                   m2.smread();
                   break;
          case 2 :
                   cout<<"\nFirst matrix: \n";
                   m1.smdisplay();
                   cout<<"\nsecond matrix: \n";
                   m2.smdisplay ();
                   break;
          case 3 :
                   cout<<"\nFirst matrix: \n";
                   m1.smdisplay();
                   cout<<"\n Transpose of the first matrix";
                   m1.smtranspose(m3);
                   m3.smdisplay();
                   cout<<"\n\nPress any key to continue...........";
                   getch();
                   clrscr();
                   cout<<"\nSecond matrix: \n";
                   m2.smdisplay();
                   cout<<"\n Transpose of the second matrix";
                   m2.smtranspose(m3);
                   m3.smdisplay();
                   break;
          case 4 :
                   if((m1.getrows()!=m2.getrows())||(m1.getcols()!=m2.getcols()))
{        cout<<"\n Matrices differ in order.Cannot perform addition\n";
                             break;
                   }
                   cout<<"Sum of \n";
                   m1.smdisplay();
                   cout<<"\n\n\n";
                   m2.smdisplay();
                   cout<<"\n\n\n";
                   m3.smadd(m1,m2);
                   m3.smdisplay();
                   break;
          case 5 :
                   if((m1.getrows()!=m2.getrows())||(m1.getcols()!=m2.getcols()))
{cout<<"\n Matrices differ in order.Cannot perform substraction\n";
                   break;
                   }
                   cout<<"Difference of \n";
                   m1.smdisplay();
                   cout<<"\n\n\n";
                   m2.smdisplay();
                   cout<<"\n\n\n";
                   m3.smsubstract(m1,m2);
                   m3.smdisplay();
                   break;
}
          cout<<"\nPress any key to continue............";
          getch();
     }
}

No comments:

Post a Comment