TEMPLATE
Templates in C++
A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass the data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘type name’. The second keyword can always be replaced by the keyword ‘class’.
PROGRAM
Write a program in C++ using function/class 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>
using namespace std;
template<class T>
void accept(T a[3][3],int r,int c)
{
int i,j;
cout<<"Enter matrix elements";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
template<class T>
void display(T a[3][3],int r,int c)
{
int i,j;
cout<<"matrix is\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j];
cout<<" ";
}
cout<<"\n";
}
}
template<class T>
void add(T a[3][3],T b[3][3],T d[3][3],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
display(d,r,c);
}
template<class T>
void sub(T a[3][3],T b[3][3],T d[3][3],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
display(d,r,c);
}
template<class T>
void multi(T a[3][3],T b[3][3],T d[3][3],int r,int c)
{
int i,j,k;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
for(k=0;k<r;k++)
{
d[i][j]=d[i][j]+a[i][k]*b[k][j];
}
}
}
display(d,r,c);
}
int main()
{
int a[3][3],b[3][3],d[3][3],r,c,n=0,m;
float e[3][3],f[3][3],g[3][3];
cout<<"Enter no of rows and column";
cin>>r>>c;
while(n!=3)
{
if(r==c)
{
cout<<"Enter 1 for int matrix\nEnter 2 for float matrix\n";
cin>>m;
if(m==1)
{
cout<<"Enter 1 matrix\n";
accept(a,r,c);
cout<<"Enter 2 matrix\n";
accept(b,r,c);
cout<<"Enter 1 for add\nEnter 2 for sub\nEnter 3 for mul\n";
cin>>n;
switch(n)
{
case 1:add(a,b,d,r,c);
break;
case 2:sub(a,b,d,r,c);
break;
case 3:multi(a,b,d,r,c);
break;
}
}
else
{
cout<<"Enter 1 matrix\n";
accept(e,r,c);
cout<<"Enter 2 matrix\n";
accept(f,r,c);
cout<<"Enter 1 for add\nEnter 2 for sub\nEnter 3 for mul\n";
cin>>n;
switch(n)
{
case 1:add(e,f,g,r,c);
break;
case 2:sub(e,f,g,r,c);
break;
case 3:multi(e,f,g,r,c);
break;
}
}
}
else
{
cout<<"Not possible";
}
}
return 0;
}
OUTPUT:
Enter no of rows and column
3
3
Enter 1 for int matrix
Enter 2 for float matrix
1
Enter 1 matrix
Enter matrix elements
1 2 3
3 4 5
6 7 8
Enter 2 matrix
Enter matrix elements
3 1 2
6 5 4
7 6 4
Enter 1 for add
Enter 2 for sub
Enter 3 for mul
1
matrix is
4 3 5
9 9 9
13 13 12
Enter 1 for int matrix
Enter 2 for float matrix
2
Enter 1 matrix
Enter matrix elements
2.2 3.3 4.3
6.5 4.4 2.3
7.4 3.2 8.5
Enter 2 matrix
Enter matrix elements
4.4 5.5 4.4
3.3 2.3 4.3
2.3 3.4 6.4
Enter 1 for add
Enter 2 for sub
Enter 3 for mul
1
matrix is
6.6 8.8 8.7
9.8 6.7 6.6
9.7 6.6 14.9
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.