Subtract two matrices - Program and concept
The concept and the program to subtract one matrix from another is similar to the addition of two matrices expect for the replacement of sign '+' with '-'.
Subtraction of matrices is only possible if the two matrices are ALIKE in nature i.e. have same number of rows and columns.
Subtraction of matrices is only possible if the two matrices are ALIKE in nature i.e. have same number of rows and columns.
Running program code:
#include<iostream.h>
#include<process.h>
#include<iomanip.h>
#include<conio.h>
const int max=10;
void main()
{
clrscr();
char ch;
int A[max][max],B[max][max],C[max][max],m,n,i,j;
cout<<"\n\n\t\tTHIS WILL Subtract two matrices\t\t\n"<<endl;
cout<<"\n\nFor subtracting two matrices, both should be ALIKE...";
cout<<" and of equal dimension\n"<<endl;
cout<<"\nEnter column of the matrix : ";
cin>>m;
cout<<"\nEnter row of the matrix : ";
cin>>n;
cout<<endl;
cout<<"\n\nEnter the FIRST matix (row wise) : "<<endl;
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
{
cout<<"\nElement at position ["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}
}
cout<<"\n\nThe FIRST matrix is : \n\n";
for(i=0; i<m; ++i)
{
cout<<"\n\t\t";
for(j=0; j<n; ++j)
{
cout<<setw(5)<<A[i][j];
}
cout<<"\n\n";
}
cout<<endl;
cout<<"\n\nEnter the SECOND matix (row wise) : "<<endl;
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
{
cout<<"\nElement at position ["<<i<<"]["<<j<<"] : ";
cin>>B[i][j];
}
}
cout<<"\n\nThe SECOND matrix is : \n\n";
for(i=0; i<m; ++i)
{
cout<<"\n\t\t";
for(j=0; j<n; ++j)
{
cout<<setw(5)<<B[i][j];
}
cout<<"\n\n";
}
cout<<endl;
for(i=0; i<m; ++i)
for(j=0; j<n; ++j)
C[i][j]=A[i][j]-B[i][j];
cout<<"\n\nThe resultant matrix is : \n";
for(i=0; i<m; ++i)
{
cout<<"\n\t\t";
for(j=0; j<n; ++j)
{
cout<<setw(5)<<C[i][j];
}
cout<<"\n\n";
}
getch();
}
Comments
Post a Comment