Featured Post

Matrix Add,Subtract,multiplication Program using Operator Overloading +,-,*

The Program Has been designed in Visual Studio 2010 with performance analysis....
The matrix

#include"iostream"
#include"conio.h"
#include"cctype"
using namespace std;
int i=0,j=0,l=0;
int e,f;
char ch;
void menu();
void choice(char);
int main();
void start();
class MAT
{
int **a;
int m,n;
public:
MAT()
{
}
MAT(int ,int);
void input();
MAT operator+(MAT);
MAT operator-(MAT);
MAT operator*(MAT);
void reentervalues();
void display();
}x,y;
//Parameterized Constructor for Dynamic Initialization of Two Dimensional Array
MAT::MAT(int c,int d)
{
m=c;
n=d;
a=new int *[m];
for(i=0;i
{
a[i]=new int[n];
}
}
//Function to take values from user
void MAT::input()
{
cout<<"Enter the Values for Matrix"<
for(i=0;i
{
for(j=0;j
{
cout<<"\nEnter The Value at "<<<<" Place:\t";
cin>>a[i][j];
}
}
l++;
system("CLS");
}
//Operator + overloaded to add two objects x+y i.e. two matrices
MAT MAT::operator+(MAT c)
{
if(x.m==y.m && x.n==y.n)
{
c=MAT(x.m,x.n);
for(i=0;i
{
for(j=0;j
{
c.a[i][j]=x.a[i][j]+y.a[i][j];
}
}
return c;
}
else
{
cout<<"\nFor two matrices to be added/subtracted the No. of Rows and Columns of Both the matrix Should be equal\n\n";
x.reentervalues();
}
}
//Operator - overloaded to subtract two objects x-y i.e. two matrices
MAT MAT::operator-(MAT c)
{
if(x.m==y.m && x.n==y.n)
{
c=MAT(x.m,x.n);
for(i=0;i
{
for(j=0;j
{
c.a[i][j]=x.a[i][j]-y.a[i][j];
}
}
return c;
}
else
{
cout<<"\nFor two matrices to be added/subtracted the No. of Rows and Columns of Both the matrix Should be equal\n\n";
x.reentervalues();
}
}
//Operator * overloaded to multiply two objects x*y i.e. two matrices
MAT MAT::operator*(MAT c)
{
int k=0;
if(x.n==y.m)
{
c=MAT(x.m,y.n);
for(i=0;i
{
for(k=0;k
{
c.a[i][k]=0;
for(j=0;j
{
c.a[i][k]=(x.a[i][j]*y.a[j][k])+c.a[i][k];
}
}
}
return c;
}
else
{
cout<<"The Matrices Multiplication cannot be proceeded as:\n For Matrices Multiplication to take place the no. of columns of matrix 1 should be equal to no. of rows of matrix 2\n\n";
x.reentervalues();
}
}
//Function to display the Matrices
void MAT::display()
{
cout<<"\n\n";
for(i=0;i
{
for(j=0;j
cout<<"\t"<
cout<<"\n";
}
}
void MAT::reentervalues()
{
cout<<"Do you want to re-enter the Rows and Column of matrixsY/N";
ch=_getch();
ch=toupper(ch);
if(ch=='Y')
start();
else
exit(0);
}
void menu()
{
cout<<"\n\t\t\t\t\tMenu\n\t\t\tPress 1 to ADD two matrices\n\t\t\tPress 2 to Subtract two matrices\n\t\t\tPress 3 to multiply two matrices\n\t\t\tPress 4 to Re-enter values\n\t\t\tPress 5 for exit\n\t\t\t";
ch=_getch();
cout<<"";
system("CLS");
choice(ch);
}
void choice(char a)
{
MAT l;
switch(a)
{
case '1':
{
l=MAT(e,f);
l=x+y;
cout<<"Matrices Enter By you";
x.display();
cout<<"\n\t+";
y.display();
cout<<"\n\tOutput=";
l.display();
menu();
break;
}
case '2':
{
l=MAT(e,f);
l=x-y;
cout<<"Matrices Enter By you";
x.display();
cout<<"\n\t-";
y.display();
cout<<"\n\tOutput=";
l.display();
menu();
break;
}
case '3':
{
l=MAT(e,f);
l=x*y;
cout<<"Matrices Enter By you";
x.display();
cout<<"\n\t*";
y.display();
cout<<"\n\tOutput=";
l.display();
menu();
break;
}
case '4':
{
start();
break;
}
case '5':
{
exit(0);
break;
}
default:
cout<<"\n\nWrong Choice enter";
menu();
}
}
void start()
{
l=0;
cout<<"\nEnter the Rows and Columns of Matrix 1:";
cin>>e>>f;
x=MAT(e,f);
cout<<"\nEnter the Rows and Columns of Matrix 2:";
cin>>e>>f;
y=MAT(e,f);
x.input();
y.input();
menu();
}
int main()
{
cout<<"\nThe Program can be used to :\n\n\t\t
ch=_getch();
if(ch)
{
system("CLS");
start();
}
return 0;
}

Comments

Related Posts

Logic Bomb...Beware!!!

Microsoft New Windows Surface 2 or Windows 8 User Interface on Display....

Internet Explorer Turns 15

Scrum Roles (Scrum Master, Product owner, Development Team) and collaboration between each other