OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.
Showing posts with label Matrix add. Show all posts
Showing posts with label Matrix add. Show all posts

Wednesday, August 4, 2010

Program 6 -Operator Overloading illustrated by Matrix Addition,Subtraction and Multiplication Program

The following program implements Function Overloading by doing addition,subtraction and multiplication of Matrices.

#include<iostream.h>
#include<iomanio.h>


const int MAXROW=3,MAXCOL=3;
struct matrix
{
         int arr[MAXROW][MAXCOL];
};


matrix operator+(matrix a,matrix b);
matrix operator-(matrix a,matrix b);
matrix operator*(matrix a,matrix b);
void mat_print(matrix p);


void main( )
{
           matrix a = {
                                    1,2,3,
                                    4,5,6,
                                    7,8,9,
                              };
           matrix b = {
                                     1,2,3,
                                     4,5,6,
                                     7,8,9,
                               };


           matrix c,d,e,f;


          c=a+b;
          d=a*b;
          e=a+b*c;
          f=a-b*c+d;


          mat_print(c);
          mat_print(d);
          mat_print(e);
          mat_print(f);


}


matrix operator+(matrix a,matrix b)
{
          matrix c;
          int i,j;


          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                                c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
            }
         return c;
}



matrix operator-(matrix a,matrix b)



{
          matrix c;
          int i,j;

          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                                c.arr[i][j] = a.arr[i][j] - b.arr[i][j];
            }
         return c;
}



matrix operator*(matrix a,matrix b)



{
          matrix c;
          int i,j,k;

          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                          {
                                     c.arr[i][j]=0;
                                     for(k=0;k<MAXCOL;k++)      
                                                    c.arr[i][j] + = a.arr[i][k] * b.arr[k][j];
                            }
            }
          return c;
}

void mat_print(matrix p)
{
            int i,j;

            cout<<endl<<endl;
            for(i=0;i<MAXROW;i++)
             {
                          cout<<endl;
                          for(j=0;j<MAXCOL;j++)
                                    cout<<setw(5)<<p.arr[i][j];
               }
}