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++ using 2d arrays
#include <iostream>
using namespace std;
int main()
{
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";
int A[r][c], B[r][c], C[r][c];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
cin >> A[i][j];
}
cout << "\n Matrix A:\n";
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << A[i][j];
cout << "\t";
}
cout << "\n";
}
cout << "\n Enter elements of Matrix B:\n";
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> B[i][j];
}
}
cout << "\n Matrix B:\n";
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << B[i][j];
cout << "\t";
}
cout << "\n";
}
cout << "\n Subtraction of Matrix B from A:\n";
for (int i = 0; i < r; i++)
{
for (int j = 0; 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.
Subtract two matrices using vectors
To subtract two matrices in C++, you can follow these steps:
- Create two 2d vectors of the same size.
- Iterate over the matrices using nested for loops. The outer loop will iterate over the rows and the inner loop will iterate over the columns.
- At each iteration, subtract the elements of the second matrix from the corresponding elements of the first matrix and store the result in a new matrix.
- Return the new matrix as the result of the subtraction.
Here is an example of how you might implement this in C++:
#include <iostream>
#include <vector>
using namespace std;
// Function to subtract two matrices
vector<vector<int>> subtract(vector<vector<int>> &num1, vector<vector<int>> &num2)
{
// Subtract the matrices
int rows = num1.size(), cols = num1[0].size();
vector<vector<int>> res(rows, vector<int>(cols, 0));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
res[i][j] = num1[i][j] - num2[i][j];
}
return res;
}
int main()
{
int rows, cols;
cin >> rows >> cols;
vector<vector<int>> num1(rows, vector<int>(cols, 0));
vector<vector<int>> num2(rows, vector<int>(cols, 0));
cout<<"Enter elements of 1st Matrix: ";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> num1[i][j];
}
}
cout<<"Enter elements of 2nd Matrix: ";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> num2[i][j];
}
}
vector<vector<int>> ans = subtract(num1, num2);
// print the resultant matrix
for (auto i : ans)
{
for (auto j : i)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}
Questions and Answers:
Using nested for loop, we subtract the corresponding elements of two matrices and store these values in another matrix.
We display a matrix using nested for loop statementfor (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << A[i][j];
cout << "\t";
}
cout << "\n";
}
Where r is the number of rows of matrices A & B and c is the number of columns of matrices A & B
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