Matrix operations using templates

Program Description

Basic matrix operations are implemented using function templates. Function templates make the functions, independent of any type.

In this example the add(),sub(),mul() functions are type independent. The compiler automatically generates the appropriate code by examining the function argument during compilation.

Thus we don't have to write the same code multiple times to support different types,the compiler automatically handles everything.

Program
/**********************************************
*
*    Matrix operations using templates
*	
*    code.cheraus.com
*
**********************************************/

#include<iostream>

using namespace std;


//class template declaration
template <class t>
class mat
{
  public:
  int i,j,m,n,p,q;
  t mat1[15][15],mat2[15][15],mat3[15][15];
  
  //function declarations
  void reada();
  void readb();
  void add();
  void sub();
  void mul();
};


/*
Function template definition for sub()

This functions computes the difference of two matrices if it is valid.

*/
template <class t> void mat<t>::sub()
{
  if(m==p && n==q)
   {
     for(i=0;i<m;i++)
      for(j=0;j<n;j++)
        mat3[i][j]=mat1[i][j]-mat2[i][j];
    cout<<"\nDifference is :\n";
    for(i=0;i<n;i++)
    {
     for(j=0;j<n;j++)
     {
       cout<<"\t"<<mat3[i][j];
     }
     cout<<"\n";
   }

   }
  else
   {
     cout<<"\nSubtraction is not possible\n";
   }

}

/*
Function template definition for mul()

This functions computes the product of two matrices if it is valid.

*/

template <class t> void mat<t>::mul()
{
 
 if(n==p)
  {
    int k;
    for(i=0;i<m;i++)
    {
      for(j=0;j<q;j++)
        {
        mat3[i][j]=0;
         for(k=0;k<n;k++)
         {
            mat3[i][j] = mat3[i][j] + mat1[i][k]*mat2[k][j];
         }
       }
     }
    cout<<"\nMultiplication is : \n";
    for(i=0;i<m;i++)
     {
      for(j=0;j<q;j++)
       {
        cout<<mat3[i][j]<<" ";
       }
       cout<<"\n";
      }
  }
 else
  {
   cout<<"\nMultiplication is not possible\n";
  }
 
}


/*
Function template definition for add()

This functions computes the sum of two matrices if it is valid.

*/
template <class t> void mat<t>::add()
{
  if(m==p && n==q)
   {
     for(i=0;i<m;i++)
      for(j=0;j<n;j++)
        mat3[i][j]=mat1[i][j]+mat2[i][j];
    cout<<"\nAddition is :\n";
    for(i=0;i<n;i++)
    {
     for(j=0;j<n;j++)
     {
       cout<<"\t"<<mat3[i][j];
     }
     cout<<"\n";
   }

   }
  else
   {
     cout<<"\nAddition is not possible\n";
   }
}


/*
Function template definition for reada()

This functions reads matrix elements.

*/


template <class t> void mat<t>::reada()
{
  cout<<"\nEnter row and column of first matrix : ";
  cin>>m>>n;
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
   {
    cout<<"\nEnter the element mat1["<<i<<"]["<<j<<"] : ";
    cin>>mat1[i][j];
   }
  cout<<"\nMatrix A is \n";
  for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
     {
       cout<<"\t"<<mat1[i][j];
     }
     cout<<"\n";
   }
}



/*
Function template definition for reada()

This functions reads matrix elements.

*/

template <class t> void mat<t>::readb()
{
  cout<<"\nEnter row and column of second matrix : ";
  cin>>p>>q;
  for(i=0;i<p;i++)
  for(j=0;j<q;j++)
   {
    cout<<"\nEnter the element mat2["<<i<<"]["<<j<<"] : ";
    cin>>mat2[i][j];
   }
  cout<<"\nMatrix B is \n";
  for(i=0;i<p;i++)
   {
     for(j=0;j<q;j++)
     {
       cout<<"\t"<<mat2[i][j];
     }
     cout<<"\n";
   }
}




int main()
{
  mat<int> a;
  mat<float> b;
  int opt,opt1;
  do{
  cout<<"\n----Main Menu----\n1.Add\n2.Sub\n3.Mul";
  cout<<"\n4.Exit\n\tEnter the choice : ";
  cin>>opt;
  switch(opt)
  {
     case 1:
             cout<<"\n1.Integer\n2.Float";
             cin>>opt1;
             switch(opt1)
              {
                 case 1:
                          a.reada();
                          a.readb();
                          a.add();
                          break;
                case 2:
                          b.reada();
                          b.readb();
                          b.add();
                          break;
              }
              break;
     case 2:
              cout<<"\n1.Integer\n2.Float";
             cin>>opt1;
             switch(opt1)
              {
                 case 1:
                          a.reada();
                          a.readb();
                          a.sub();
                          break;      
                case 2:
                          b.reada();
                          b.readb();
                          b.sub();
                          break;
              }
              break;
     case 3:
             cout<<"\n1.Integer\n2.Float";
             cin>>opt1;
             switch(opt1)
              {
                 case 1:
                          a.reada();
                          a.readb();
                          a.mul();
                          break;      
                case 2:
                          b.reada();
                          b.readb();
                          b.mul();
                          break;
              }
              break;
     case 4: break;
  }
}while(opt!=4);
return 0;
}

Output

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 1

1.Integer
2.Float1

Enter row and column of first matrix : 3
3

Enter the element mat1[0][0] : 1

Enter the element mat1[0][1] : 11

Enter the element mat1[0][2] : 21

Enter the element mat1[1][0] : 2

Enter the element mat1[1][1] : 1

Enter the element mat1[1][2] : 2

Enter the element mat1[2][0] : 4

Enter the element mat1[2][1] : 34

Enter the element mat1[2][2] : 34

Matrix A is
1 11 21
2 1 2
4 34 34

Enter row and column of second matrix : 3
3

Enter the element mat2[0][0] : 1

Enter the element mat2[0][1] : 1

Enter the element mat2[0][2] : 32

Enter the element mat2[1][0] : 43

Enter the element mat2[1][1] : 43

Enter the element mat2[1][2] : 3

Enter the element mat2[2][0] : 434

Enter the element mat2[2][1] : 3

Enter the element mat2[2][2] : 43

Matrix B is
1 1 32
43 43 3
434 3 43

Addition is :
2 12 53
45 44 5
438 37 77

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 1

1.Integer
2.Float2

Enter row and column of first matrix : 2
2

Enter the element mat1[0][0] : 1.2

Enter the element mat1[0][1] : 1.3

Enter the element mat1[1][0] : 4.4

Enter the element mat1[1][1] : 5.5

Matrix A is
1.2 1.3
4.4 5.5

Enter row and column of second matrix : 2
2

Enter the element mat2[0][0] : 4

Enter the element mat2[0][1] : 5

Enter the element mat2[1][0] : 3.3

Enter the element mat2[1][1] : 4.45

Matrix B is
4 5
3.3 4.45

Addition is :
5.2 6.3
7.7 9.95

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 1

1.Integer
2.Float2

Enter row and column of first matrix : 2
1

Enter the element mat1[0][0] : 1

Enter the element mat1[1][0] : 2

Matrix A is
1
2

Enter row and column of second matrix : 2
2

Enter the element mat2[0][0] : 1

Enter the element mat2[0][1] : 2

Enter the element mat2[1][0] : 3

Enter the element mat2[1][1] : 4

Matrix B is
1 2
3 4

Addition is not possible

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 2

1.Integer
2.Float3

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 2

1.Integer
2.Float2

Enter row and column of first matrix : 1
2

Enter the element mat1[0][0] : 3

Enter the element mat1[0][1] : 4

Matrix A is
3 4

Enter row and column of second matrix : 5
1

Enter the element mat2[0][0] : 1

Enter the element mat2[1][0] : 1

Enter the element mat2[2][0] : 12

Enter the element mat2[3][0] : 3

Enter the element mat2[4][0] : 4

Matrix B is
1
1
12
3
4

Subtraction is not possible

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 2

1.Integer
2.Float2

Enter row and column of first matrix : 2
2

Enter the element mat1[0][0] : 1.2

Enter the element mat1[0][1] : 1.1

Enter the element mat1[1][0] : 1.3

Enter the element mat1[1][1] : 3.3

Matrix A is
1.2 1.1
1.3 3.3

Enter row and column of second matrix : 2
2

Enter the element mat2[0][0] : 1

Enter the element mat2[0][1] : 2

Enter the element mat2[1][0] : 3

Enter the element mat2[1][1] : 4

Matrix B is
1 2
3 4

Difference is :
0.2 -0.9
-1.7 -0.7

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 3

1.Integer
2.Float1

Enter row and column of first matrix : 3
3

Enter the element mat1[0][0] : 1

Enter the element mat1[0][1] : 2

Enter the element mat1[0][2] : 3

Enter the element mat1[1][0] : 3

Enter the element mat1[1][1] : 4

Enter the element mat1[1][2] : 5

Enter the element mat1[2][0] : 6

Enter the element mat1[2][1] : 7

Enter the element mat1[2][2] : 8

Matrix A is
1 2 3
3 4 5
6 7 8

Enter row and column of second matrix : 3
1

Enter the element mat2[0][0] : 1

Enter the element mat2[1][0] : 2

Enter the element mat2[2][0] : 3

Matrix B is
1
2
3

Multiplication is :
14
26
44

----Main Menu----
1.Add
2.Sub
3.Mul
4.Exit
Enter the choice : 4

Comments
comments powered by Disqus