Addition Subtraction and Multiplication of Matrix in C++
Today we will learn how to do “Addition Subtraction and Multiplication of Matrix in c++“. What is the Matrix? What is the Rules of Addition , Subtraction and Multiplication of Matrix? Implementation of Addition,Subtraction and Multiplication of Matrix in C++ programming language.
What is the Matrix :-
The Numerical data which is written in the shape of Columns and Rows into Square brackets.It just like a Two dimensional Array.Every Matrix have its own order. The order of a Matrix is Number of Rows *Number of Columns.
The Formula of order of a Matrix = “m*n”.here m is use to represent No. of Rows and n is No. of Columns
Note : – We don’t multiply the No. of Rows into No.of Columns. The order of a Matrix is just tells that how many rows and columns are exits in this Matrix. For Example 3*3. It is also read as “3 by 3” or “2 by 2” etc….
For Example
Rules of those operations of Matrix :-
- The Addition are performed only those two Matrices which have same order.
- The Subtraction are performed only those two Matrices which have same order.
- The Multiplication are performed on Matrices if and only if the column of 1st matrix is equal to Rows of 2nd Matrix. For Example :-
Program of Addition,Subtraction & Multiplication in C/C++ Language
#include<iostream>
using namespace std;
class Matrix
{
private:
int i,j,k,M1[3][3],M2[3][3],r,result[3][3];
public:
void create()
{
cout<<“\tEnter the values of First matrix”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<” Enter the number of “<<i+1<<” Row and “<<j+1<<” Column\t”;
cin>>M1[i][j];
}
}
cout<<“\tEnter the values of Second Matrix”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<” Enter the number of “<<i+1<<” Row and “<<j+1<<” Column\t”;
cin>>M2[i][j];
}
}
cout<<“The values of First Matirx is”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<M1[i][j]<<“\t”;
}
cout<<endl;
}
cout<<“The values of Second Matirx is”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<M2[i][j]<<“\t”;
}
cout<<endl;
}
}
void ADD()
{
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=M1[i][j]+M2[i][j];
}
}
cout<<endl<<“\t\tThe Addtion of these two Matrix are as follows…”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<“\t”<<result[i][j]<<“\t”;
}
cout<<endl;
}
}
void Subtraction()
{
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=M1[i][j]-M2[i][j];
}
}
cout<<endl<<“\t\tThe Subtraction of these two Matrix are as follows…”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<“\t”<<result[i][j]<<“\t”;
}
cout<<endl;
}
}
void Multiplication()
{
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=0;
}
}
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
r=M1[i][k]*M2[k][j];
result[i][j]=result[i][j]+r;
}
}
}
cout<<endl<<“\t\tThe Multiplication of these two Matrix are as follow…”<<endl;
for( i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<“\t”<<result[i][j]<<“\t”;
}
cout<<endl;
}
}
};
int main()
{
Matrix a;
a.create();
a.ADD();
a.Subtraction();
a.Multiplication();
}
you may also like to read Abrar UL Haq Arrested in Dubai Pakistan Singer and PTI Leader.and also Implementation of Queue using Link List Briefly program in C++.