/*
Problem Statement:
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
#include
#include
template //Template Definition
class matrix
{
int m,n;
T a[10][10];
public:
matrix() //Default Constructor
{
m=n=0;
}
matrix(int x,int y,T c[10][10]); //Parameterised Constructor
void insert();
void display();
matrix operator +(matrix);
matrix operator -(matrix);
matrix operator *(matrix);
};
//------------------- Constructor ----------
template
matrix::matrix(int x,int y,int z[10][10])
{
int i,j;
m=x;
n=y;
for(i=0;i for(j=0;j a[i][j]=z[i][j];
}
//------------------ Insertion --------------
template
void matrix::insert()
{
cout<<"\n Enter the No Of Rows & Columns : ";
cin>>m>>n;
cout<<"\n Enter Matrix : ";
for(int i=0;i for(int j=0;j {
cout<<"\n Enter Element "< cin>>a[i][j];
}
}
//------------------ Addition --------------
template
matrix matrix::operator +(matrix b)
{
T c[10][10];
int x,y;
x=m;
y=n;
if( m==b.m && n==b.n)
{
for(int i=0;i for(int j=0;j c[i][j]=a[i][j]+b.a[i][j];
}
else
cout<<"\n Addition is Not Possible as No of Rows & Columns Must be Same";
return matrix(x,y,c);
}
//------------------ Substraction --------------
template
matrix matrix::operator -(matrix b)
{
T c[10][10];
int x,y;
x=m;
y=n;
if(m==b.m && n==b.n)
{
for(int i=0;i for(int j=0;j c[i][j]=a[i][j]-b.a[i][j];
}
else
cout<<"\n Addition is Not Possible as No of Rows & Columns Must be Same";
return matrix(x,y,c);
}
//------------------ Multiplication --------------
template
matrix matrix::operator *(matrix b)
{
T c[10][10];
int i,j,k,x,y;
x=m;
y=b.n;
if(n==b.m)
{
for(i=0;i {
for(j=0;j {
c[i][j]=0;
for(k=0;k c[i][j]=c[i][j]+a[i][k]*b.a[k][j];
}
}
}
else
cout<<"\n Multiplication is Not Possible as No of Rows & Columns Must be Same";
return matrix(x,y,c);
}
//------------------ Display --------------
template
void matrix::display()
{
cout<<"\n\n Matrix is : ";
for(int i=0;i {
cout<<"\n";
for(int j=0;j {
cout< }
}
}
void main()
{
clrscr();
int choice;
char ans;
matrix m1,m2,m3;
do
{
cout<<"\n-----Menu----";
cout<<"\n 1.Enter 1st Matrix \n 2.Enter 2nd Matrix \n 3.Perform Addition";
cout<<"\n 4.Perform Substraction \n 5.Perform Multiplication \n 6.Exit";
cout<<"\n Enter Choice Your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
m1.insert();
m1.display();
break;
case 2:
m2.insert();
m2.display();
break;
case 3:
m3=m1+m2;
m3.display();
break;
case 4:
m3=m1-m2;
m3.display();
break;
case 5:
m3=m1*m2;
m3.display();
break;
case 6:
exit(1);
} cout<<"\n\n Do You Want To Continue ?(Y/N) : ";
ans=getche();
} while(ans=='y'||ans=='Y');
getch();
}
Problem Statement:
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
#include
#include
template
class matrix
{
int m,n;
T a[10][10];
public:
matrix() //Default Constructor
{
m=n=0;
}
matrix(int x,int y,T c[10][10]); //Parameterised Constructor
void insert();
void display();
matrix
matrix
matrix
};
//------------------- Constructor ----------
template
matrix
{
int i,j;
m=x;
n=y;
for(i=0;i
}
//------------------ Insertion --------------
template
void matrix
{
cout<<"\n Enter the No Of Rows & Columns : ";
cin>>m>>n;
cout<<"\n Enter Matrix : ";
for(int i=0;i
cout<<"\n Enter Element "< cin>>a[i][j];
}
}
//------------------ Addition --------------
template
matrix
{
T c[10][10];
int x,y;
x=m;
y=n;
if( m==b.m && n==b.n)
{
for(int i=0;i
}
else
cout<<"\n Addition is Not Possible as No of Rows & Columns Must be Same";
return matrix
}
//------------------ Substraction --------------
template
matrix
{
T c[10][10];
int x,y;
x=m;
y=n;
if(m==b.m && n==b.n)
{
for(int i=0;i
}
else
cout<<"\n Addition is Not Possible as No of Rows & Columns Must be Same";
return matrix
}
//------------------ Multiplication --------------
template
matrix
{
T c[10][10];
int i,j,k,x,y;
x=m;
y=b.n;
if(n==b.m)
{
for(i=0;i
for(j=0;j
c[i][j]=0;
for(k=0;k
}
}
}
else
cout<<"\n Multiplication is Not Possible as No of Rows & Columns Must be Same";
return matrix
}
//------------------ Display --------------
template
void matrix
{
cout<<"\n\n Matrix is : ";
for(int i=0;i
cout<<"\n";
for(int j=0;j
cout< }
}
}
void main()
{
clrscr();
int choice;
char ans;
matrix
do
{
cout<<"\n-----Menu----";
cout<<"\n 1.Enter 1st Matrix \n 2.Enter 2nd Matrix \n 3.Perform Addition";
cout<<"\n 4.Perform Substraction \n 5.Perform Multiplication \n 6.Exit";
cout<<"\n Enter Choice Your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
m1.insert();
m1.display();
break;
case 2:
m2.insert();
m2.display();
break;
case 3:
m3=m1+m2;
m3.display();
break;
case 4:
m3=m1-m2;
m3.display();
break;
case 5:
m3=m1*m2;
m3.display();
break;
case 6:
exit(1);
} cout<<"\n\n Do You Want To Continue ?(Y/N) : ";
ans=getche();
} while(ans=='y'||ans=='Y');
getch();
}
Comments
Post a Comment