SUBTRACTION OF TWO MATRICES USING ARRAY C++ PROGRAM And ALGORITHM
Algorithm
Subtraction of two matrixes:
Rule: Subtraction of two matrixes is only possible if both matrixes are of same size.
Suppose two matrixes A and B is of same size m X n
Subtraction of two marixes is defined as
(A - B)ij = Aij - Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 3 X 2 is as follow:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h> // for setw()
int main()
{
int m1[10][10],m2[10][10],add[10][10],i,j,r1,c1,c2,r2;
clrscr();
start:
cout<<"Enter how row and column you wants in matrix 1:\n";
cin>>r1>>c1;
cout<<"Enter how row and column you wants in matrix 2:\n";
cin>>r2>>c2;
if(r1!=r2 || c1!=c2)
{
cout<<"Enter same m*n matrixs.\n";
goto start;
}
cout<<"Enter for first matrix:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter for sencond matrix:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>m2[i][j];
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]-m2[i][j];
}
}
cout<<"The Substraction of matrix is:\n";
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
{
cout<<setw(5)<<add[i][j];
}
}
getch();
return 0;
}
0 comments:
Post a Comment