WELCOME

Welcome to KoDes4U

Pages

Wednesday, March 9, 2011

templates(C++ program)

/*Write a C++ program 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.
Coded for KoDes4.blogspot.com*/

#include

#include

#include

template

class CMatrix
{
int Row, Col;
T a[10][10];
public:
void Input();
void AddMat(CMatrix
mat1, CMatrix mat2);
int check(CMatrix
mat2);
void SubMat(CMatrix
mat1, CMatrix mat2);
void Display();
};
template

void CMatrix
::Input()
{
cout<<"\nEnter the order for the matrix "; cin>>Row>>Col;
cout<<"\n Enter the elements:"; for(int i=0; i
>a[i][j];
}
}
}
template

void CMatrix
::Display()
{
for(int i=0; i

void CMatrix
::AddMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

void CMatrix
::SubMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

int CMatrix
::check(CMatrix mat2)
{
int m,n,p,q;
m = Row;
n = Col;
p = mat2.Row;
q = mat2.Col;
if(m==p && n==q)
return 1;
else
return 0;
}
void main()
{
int ch,op,k,r;
clrscr();
do
{
cout<<"\n"; cout<<"\n Matrix operations using templates"; cout<<"\n1.Integer\n2.float\n3.exit"; cout<<"\n Choice..."; cin>>ch;
switch(ch)
{
case 1:
CMatrix
m1, m2, res;
cout<<"\n\n Integer Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>op;
switch(op)
{
case 1:
m1.Input();
m2.Input();
k=m1.check(m2);
if(k==1)
{
res.AddMat(m1, m2);
cout<<"\n Addition of marix::"; res.Display(); } else cout<<"\n ROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: m1.Input(); m2.Input(); k=m1.check(m2); if(k==1) { res.SubMat(m1, m2); cout<<"\n The subtraction of the two matrices is"; res.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } break; case 2: CMatrix
fm1, fm2, fres;
cout<<"\n\n Float Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>r;
switch(r)
{
case 1:
fm1.Input();
fm2.Input();
k=fm1.check(fm2);
if(k==1)
{
fres.AddMat(fm1, fm2);
cout<<"\nThe addition of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: fm1.Input(); fm2.Input(); k=fm1.check(fm2); if(k==1) { fres.SubMat(fm1, fm2); cout<<"\nThe subtraction of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } } } while(ch!=3); getch(); }

No comments:

Post a Comment