Martix Addition, Subtraction and Multiplication


MATRIX USING CONSTRUCTORS

//Matrix operations using constructors

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

class matrix

{

int a[10][10],n,m;

public: matrix()

{

m=n=0;

}

matrix(int);

matrix(int,int);

void oper(matrix,matrix);

void disp();

};

matrix::matrix(int p)

{

n=m=p;

int i,j;

cout<<“\n\n Enter the matrix elements: “<<endl;

for(i=0;i<m;i++)

for(j=0;j<n;j++)

cin>>a[i][j];

void disp();

}

matrix::matrix(int p,int q)

{

n=q;

m=p;

int i,j;

cout<<“\n\n Enter the matrix elements:”<<endl;

for(i=0;i<m;i++)

for(j=0;j<n;j++)

cin>>a[i][j];

void disp();

}

void matrix::disp()

{

int i,j;

cout<<” Matrix is “<<endl;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cout<<a[i][j]<<setw(4);

cout<<endl;

}

}

void matrix::oper(matrix m1,matrix m2)

{

int sum,i,j,k;

cout<<“\n\n Addtion of matrices is “<<endl;

for(i=0;i<m1.m;i++)

{

for(j=0;j<m2.n;j++)

cout<<m1.a[i][j]+m2.a[i][j]<<setw(4);

cout<<endl;

}

cout<<“\n\n Subtraction of matrices is “<<endl;

for(i=0;i<m1.m;i++)

{

for(j=0;j<m2.n;j++)

cout<<m1.a[i][j]-m2.a[i][j]<<setw(4);

cout<<endl;

}

cout<<“\n\n Multiplication  of matrices is “<<endl;

for(i=0;i<m1.m;i++)

{

for(j=0;j<m2.n;j++)

{

sum=0;

for(k=0;k<m1.n;k++)

sum+=m1.a[i][k]*m2.a[k][j];

cout<<sum<<setw(4);

}

cout<<endl;

}

}

void main()

{

int a1,b1,a2,b2;

clrscr();

matrix n1;

cout<<“\n\t\t Matrix operations using constructors:”;

cout<<“\n\t\t ~~~~~~ ~~~~~~~~~~ ~~~~~ ~~~~~~~~~~~~~”;

cout<<“\n\n Enter the order of 1st matrix : “;

cin>>a1>>b1;

cout<<“\n Enter the order of 2nd matrix : ” ;

cin>>a2>>b2;

if(a1==a2&&b1==b2&&a2==b1)

{

matrix n2(a1);

matrix n3(a2,b2);

n1.oper(n2,n3);

}

else

cout<<“\n Opertion is not possible”;

getch();

}

Advertisements