Develop a library for
performing various Matrix operations. Use templates to make them generalized
for any data type.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k;
template<class T>
class M
{
T a[3][3];
public:
M()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=0;
}
void display();
void insert();
M operator +(M &);
M operator -(M &);
M operator *(M &);
M operator ||(M &);
};
//DISPLAY
template<class T>
void M<T>::display()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
//INSERT
template<class T>
void M<T>::insert()
{
cout<<"Enter The Elements :";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
template<class T> //ADDITION
M<T> M<T>::operator + (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
r.a[i][j]=a1.a[i][j]+a[i][j];
return r;
}
template<class T> //SUBTRACTION
M<T> M<T>::operator - (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
r.a[i][j]=a1.a[i][j]-a[i][j];
return r;
}
template<class T> //MULTIPLICATIO N
M<T> M<T>::operator * (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
r.a[i][j]=0;
for(k=0;k<3;k++)
{
r.a[i][j]=a1.a[i][k]*a[k][j]+r.a[i][j];
}
}
return r;
}
template<class T> //TRANSPOSE
M<T> M<T>::operator || (M<T> &a1)
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=a1.a[j][i];
return *this;
}
void main()
{
clrscr();
int choice;
M <double>m1;
M <double>m2;
M <double>m3;
while(1)
{ getch();
clrscr();
cout<<endl<<":::::::::::MATRIX OPERATIONS:::::::::::"<<endl;
cout<<"1.Display\n2.Insert\n3.Addition\n4.Subtraction\n5.Multiplication\n6.Transpose\n7.EXIT";
cout<<"\nEnter your Choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"MATRIX 1\n";
m1.display();
cout<<"MATRIX 2\n";
m2.display();
//cout<<"MATRIX 3\n";
//m3.display();
break;
case 2:
cout<<"Insert in Matrix 1.\n";
m1.insert();
cout<<"Insert in Matrix 2.\n";
m2.insert();
break;
case 3:
cout<<"Addition is : \n";
m3=m1+m2;
m3.display();
break;
case 4:
cout<<"Subtraction is : \n";
m3=m1-m2;
m3.display();
break;
case 5:
cout<<"Multiplication is : \n";
m3=m1*m2;
m3.display();
break;
case 6:
cout<<"Transpose is : \n";
m3||m1;
m3.display();
break;
case 7: clrscr();
cout<<"\n\n\n\n\n\n\t\t\tThanks for Using...\n";
textcolor(BLINK+RED);
cprintf("\n\n\n\t\t\tMade BY:BHAVESH POPTANI\t\t\t");
getch();
exit(0);
default:
break;
}
}
getch();
}
SOURCECODE:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k;
template<class T>
class M
{
T a[3][3];
public:
M()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=0;
}
void display();
void insert();
M operator +(M &);
M operator -(M &);
M operator *(M &);
M operator ||(M &);
};
//DISPLAY
template<class T>
void M<T>::display()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
//INSERT
template<class T>
void M<T>::insert()
{
cout<<"Enter The Elements :";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
}
template<class T> //ADDITION
M<T> M<T>::operator + (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
r.a[i][j]=a1.a[i][j]+a[i][j];
return r;
}
template<class T> //SUBTRACTION
M<T> M<T>::operator - (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
r.a[i][j]=a1.a[i][j]-a[i][j];
return r;
}
template<class T> //MULTIPLICATIO N
M<T> M<T>::operator * (M<T> &a1)
{
M r;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
r.a[i][j]=0;
for(k=0;k<3;k++)
{
r.a[i][j]=a1.a[i][k]*a[k][j]+r.a[i][j];
}
}
return r;
}
template<class T> //TRANSPOSE
M<T> M<T>::operator || (M<T> &a1)
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=a1.a[j][i];
return *this;
}
void main()
{
clrscr();
int choice;
M <double>m1;
M <double>m2;
M <double>m3;
while(1)
{ getch();
clrscr();
cout<<endl<<":::::::::::MATRIX OPERATIONS:::::::::::"<<endl;
cout<<"1.Display\n2.Insert\n3.Addition\n4.Subtraction\n5.Multiplication\n6.Transpose\n7.EXIT";
cout<<"\nEnter your Choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"MATRIX 1\n";
m1.display();
cout<<"MATRIX 2\n";
m2.display();
//cout<<"MATRIX 3\n";
//m3.display();
break;
case 2:
cout<<"Insert in Matrix 1.\n";
m1.insert();
cout<<"Insert in Matrix 2.\n";
m2.insert();
break;
case 3:
cout<<"Addition is : \n";
m3=m1+m2;
m3.display();
break;
case 4:
cout<<"Subtraction is : \n";
m3=m1-m2;
m3.display();
break;
case 5:
cout<<"Multiplication is : \n";
m3=m1*m2;
m3.display();
break;
case 6:
cout<<"Transpose is : \n";
m3||m1;
m3.display();
break;
case 7: clrscr();
cout<<"\n\n\n\n\n\n\t\t\tThanks for Using...\n";
textcolor(BLINK+RED);
cprintf("\n\n\n\t\t\tMade BY:BHAVESH POPTANI\t\t\t");
getch();
exit(0);
default:
break;
}
}
getch();
}
SCREENSHOTS:
No comments:
Post a Comment