Matrix Mulitplication Addition in Cpp / C++

This Cpp program is for Matrix Multiplication, Addition and Display of Matrix / Array 2D. This is a part of Mumbai University MCA Colleges C++ / CPP Programs MCA. 


In the below program we have used 2 Two Dimensional Arrays to form a Matrix and showed the different operations on Matrix.

#include<iostream.h>
#include<conio.h>
int i,j,k;

class Matrix
{
int a[2][2],b[2][2];
public :
void get_data();
void Show(int [2][2]);
void Add();
void Multi();
};

void Matrix :: get_data()
{
cout<<"Enter data for Matrix A : "<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}

cout<<"Enter data for Matrix B : "<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}
Show(a);
Show(b);
}


void Matrix :: Show(int x[2][2])
{
cout<<endl<<endl;
cout<<"Array :"<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<x[i][j];
}
cout<<endl;
}
}

void Matrix :: Add()
{
int c[2][2];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=0;

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

cout<<endl<<"Addittion : "<<endl;
Show(c);
}

void Matrix :: Multi()
{
int c[2][2];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=0;

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
cout<<endl<<"Multiplication : "<<endl;
Show(c);
}

void main()
{
Matrix m;
clrscr();
m.get_data();
m.Add();
m.Multi();
getch();
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs and Simple Programs. Happy Programming and Studying.

No comments:

Post a comment