Subtraction of two matrices in c++

Subtraction of two matrices in c++ In this program we are going to make a program for subtraction of two matrices in c++.

In this program, we are going to make a program for subtraction of two matrices in c++. To make this program, we initialize three matrices A, B & C then we insert elements of matrices A & B by the user using nested for loops. At last, we subtract the corresponding elements of matrices A & B and then we store those values in another matrix C and display all elements of matrix C.

Subtraction of two matrices in c++

#include<iostream>
using namespace std;
int main()
{
int A[3][3],B[3][3],C[3][3],i,j;
int r,c;
cout<<"\n Enter number of rows:";
cin>>r;
cout<<"\n Enter number of columns:";
cin>>c;
cout<<"\n Enter elements of Matrix A:\n";
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
cin>>A[i][j];
}
cout<<"\n Matrix A:\n"; 
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
cout<<A[i][j];
cout<<"\t";
}
cout<<"\n";
}
cout<<"\n Enter elements of Matrix B:\n";
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
cout<<"\t";
cin>>B[i][j];
}
}
cout<<"\n Matrix B:\n";
for(i=1;i<=r;i++)                    
{
for(j=1;j<=c;j++)
{
cout<<B[i][j];
cout<<"\t";
}
cout<<"\n";
}
cout<<"\n Subtraction of Matrix B from A:\n";
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
C[i][j]=A[i][j]-B[i][j];
cout<<C[i][j];
cout<<"\t";
}
cout<<"\n";
}
return 0;
}

Approach to make a program for Subtraction of two matrices in c++:

  • First, we initialize three matrices A, B and C using 2D arrays.
  • We take the number of rows and columns of both matrices A and B.
  • We insert elements of matrices A by user using nested for loops.
  • We print elements of matrices A on the output screen using nested for loop.
  • We insert elements of matrices B by user using nested for loops.
  • We print elements of matrices B on the output screen using nested for loop.
  • Using nested for loop, we subtract the corresponding elements of matrices A & B and stores these values in matrix C.
  • We print all the elements of matrix C which contains the subtraction of corresponding elements of matrices A & B.

Nested for loop statement used in this program:

for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
statement1;
statement2;
}
}
Where r= Number of rows of matrices A & B
          c= Number of columns of matrices A & B

 

Questions and Answers on subtraction of matrices in c++

How do you subtract two matrices in C++?
Using nested for loop, we subtract the corresponding elements of two matrices and store these values in another matrix.

How do you display a matrix in C++? 

 We display a matrix using nested for loop statement
cout<<"\n Matrix A:\n";
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
cout<<A[i][j];
cout<<"\t";
}
cout<<"\n";
}
Where r= Number of rows of matrices A & B
          c= Number of columns of matrices A & B
 

What is the value of the identity matrix?

It is a square matrix, whose all the diagonal elements are 1 and all the non-diagonal elements are 0.
For example, An identity matrix of order 3*3
1         0         0
0         1         0
0         0         1

Thanks for reading this program for the Subtraction of two matrices in c++. I hope you enjoy this program as much as I enjoy offering this to you. If you have any questions or queries related to this program, please don't hesitate to comment below or contact me.

Programs You may like:

C++ program for addition of two matrices

Menu-Driven Program for Array Operations in C++

Factorial program in C++

1 comment

  1. These elements make the wager distribution deviate from the log-normal distribution, which is noticed in games (A-G). The power-law tail, which was not noticed within the earlier study8, might end result from the increment of the utmost allowed pores and skin value (from $400 to $1800). Players can play at their own tempo since betting time is 카지노사이트 limitless.
Please do not enter any spam link in the comment box.