C++ program using function template to perform arithmatic operations

, by Prashant Gunjal


Write a program in C++ using function template to read two matrices of different data types such as integers and floating point values and perform simple arithmetic operations on these matrices separately and display it.



#include<iostream.h>
#include<conio.h>
template<class T>
class matrix
{
T x[3][3];
public:
void getmatrix();
void showmatrix();
void addition(matrix<T>);
void subtraction(matrix<T>);
void multiplication(matrix<T>,matrix<T>);
};
template<class T>
void matrix<T>::getmatrix()
{
int i,j;
cout<<"\n\n\t enter values of matrix";
for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
      {
cin>>x[i][j];
      }
  }
}
template<class T>
void matrix<T>::showmatrix()
{
int i,j;
cout<<"\n\n\t matrix is:";
for(i=0;i<3;i++)
  {
   cout<<"\n\n";
   for(j=0;j<3;j++)
      {
cout<<"\t"<<x[i][j];
      }
  }
}
template<class T>
void matrix<T>::addition(matrix<T> b)
{
int i,j;
for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
      {
x[i][j]=x[i][j]+b.x[i][j];
      }
   }
}
template<class T>
void matrix<T>::subtraction(matrix<T> b)
{
int i,j;
for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
      {
x[i][j]=x[i][j]-b.x[i][j];
      }
   }
}
template<class T>
void matrix<T>::multiplication(matrix<T> b,matrix<T> a)
{
int i,j,k;
for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
      {
x[i][j]=0;
      }
   }
for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
      {
for(k=0;k<3;k++)
  {
   x[i][j]=x[i][j]+(a.x[i][k]*b.x[k][j]);
  }
      }
   }
}
void main()
{
int ch;
matrix<int> a1,b1,c1;
matrix<float> a2,b2,c2;
clrscr();
do
 {
  cout<<"\n\n\t 1> Addition (int)";
  cout<<"\n\n\t 2> Subtraction (int)";
  cout<<"\n\n\t 3> Multiplication (int)";
  cout<<"\n\n\t 4> Addition (float)";
  cout<<"\n\n\t 5> Subtraction (float)";
  cout<<"\n\n\t 6> Multiplication (float)";
  cout<<"\n\n\t enter your choice";
  cin>>ch;
  switch(ch)
  {
  case 1:a1.getmatrix();
 a1.showmatrix();
 b1.getmatrix();
 b1.showmatrix();
 a1.addition(b1);
 a1.showmatrix();
 break;
  case 2:a1.getmatrix();
 a1.showmatrix();
 b1.getmatrix();
 b1.showmatrix();
 a1.subtraction(b1);
 a1.showmatrix();
 break;
  case 3:a1.getmatrix();
 a1.showmatrix();
 b1.getmatrix();
 b1.showmatrix();
 c1.multiplication(b1,a1);
 c1.showmatrix();
 break;
  case 4:a2.getmatrix();
 a2.showmatrix();
 b2.getmatrix();
 b2.showmatrix();
 a2.addition(b2);
 a2.showmatrix();
 break;
  case 5:a2.getmatrix();
 a2.showmatrix();
 b2.getmatrix();
 b2.showmatrix();
 a2.subtraction(b2);
 a2.showmatrix();
 break;
  case 6:a2.getmatrix();
 a2.showmatrix();
 b2.getmatrix();
 b2.showmatrix();
 c2.multiplication(b2,a2);
 c2.showmatrix();
 break;
  }
}while(ch!=7);
getch();
}
/* OUTPUT
1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)
enter your choice1
enter values of matrix 1 2 3 4 5 6 7 8 9
matrix is:

1       2       3
4       5       6
7       8       9
enter values of matrix 2 3 4 5 6 7 8 9 0
matrix is:

2       3       4
5       6       7
8       9       0
matrix is:
3       5       7
9       11      13
15      17      9

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)
enter your choice 2

enter values of matrix 0 9 8 7 6 5 4 3 2
matrix is:

0       9       8
7       6       5
4       3       2

enter values of matrix 1 2 3 4 5 6 7 8 9
matrix is:
1       2       3
4       5       6
7       8       9

matrix is:
-1      7       5
3       1       -1
-3      -5      -7

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)
enter your choice 3

enter values of matrix 1 2 3 4 5 6 7 8 9
matrix is:
1       2       3
4       5       6
7       8       9

enter values of matrix 2 3 4 5 6 7 8 9 0
matrix is:
2       3       4
5       6       7
8       9       0

matrix is:
36      42      18
81      96      51
126     150     84

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)

enter your choice 4

enter values of matrix 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5
matrix is:
1.5     2.5     3.5
4.5     5.5     6.5
7.5     8.5     9.5

enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0
matrix is:
1.2     2.3     3.4
4.5     5.6     6.7
7.8     8.9     9

matrix is:
2.7     4.8     6.9
9       11.1    13.2
15.3    17.4    18.5

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)
enter your choice 5

enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0
matrix is:
1.2     2.3     3.4
4.5     5.6     6.7
7.8     8.9     9

enter values of matrix 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
matrix is:
1.1     2.2     3.3
4.4     5.5     6.6
7.7     8.8     9.9

matrix is:
0.1     0.1     0.1
0.1     0.1     0.1
0.1     0.09   -0.9

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)
enter your choice 6

enter values of matrix 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
matrix is:
1.1     2.2     3.3
4.4     5.5     6.6
7.7     8.8     9.9

enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0
matrix is:
1.2     2.3     3.4
4.5     5.6     6.7
7.8     8.9     9

matrix is:
36.959999       44.219997       48.18
81.510002       99.659996       111.20999
126.059998      155.099991      174.23999

1> Addition (int)
2> Subtraction (int)
3> Multiplication (int)
4> Addition (float)
5> Subtraction (float)
6> Multiplication (float)

enter your choice 7 */

1 comments:

Anonymous said...

Thanks Darling :)