Home »
C++ Programs
C++ program to find the subtraction of two matrices using class
Submitted by Shubh Pachori, on October 03, 2022
Problem statement
Given two matrices, we have to find their subtraction using the class and object approach.
Example:
Input:
Enter 1st Matrix elements :
[0][0]:1
[0][1]:2
[0][2]:3
[1][0]:4
[1][1]:5
[1][2]:6
[2][0]:7
[2][1]:8
[2][2]:9
Enter 2nd Matrix elements :
[0][0]:1
[0][1]:2
[0][2]:3
[1][0]:4
[1][1]:5
[1][2]:6
[2][0]:7
[2][1]:8
[2][2]:9
Output:
Entered 1st Matrix :
1 2 3
4 5 6
7 8 9
Entered 2nd Matrix :
1 2 3
4 5 6
7 8 9
Subtracted Matrix :
0 0 0
0 0 0
0 0 0
C++ code to find the subtraction of two matrices using the class and object approach
#include <iostream>
using namespace std;
// create a class
class Matrix {
// private data members
private:
int matrix1[3][3], matrix2[3][3];
// public member functions
public:
// getMatrix() function to insert matrices
void getMatrix() {
cout << "Enter 1st Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix1[row][column];
}
}
cout << "\nEnter 2nd Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix2[row][column];
}
}
}
// subtractMatrix() function to subtract matrices
void subtractMatrix() {
// initialising variables to perform operations
int subtract[3][3], row, column;
cout << "\nEntered 1st Matrix :" << endl;
// nested for loop to show inputted matrices
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix1[row][column] << " ";
}
cout << "\n";
}
cout << "\nEntered 2nd Matrix :" << endl;
// nested for loop to show inputted matrices
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix2[row][column] << " ";
}
cout << "\n";
}
cout << "\n";
// nested for loop to subtract matrices
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
subtract[row][column] = matrix1[row][column] - matrix2[row][column];
}
}
cout << "\nSubtracted Matrix :" << endl;
// nested for loop to show subtracted matrices
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << subtract[row][column] << " ";
}
cout << endl;
}
}
};
int main() {
// create an object
Matrix M;
// calling getMatrix() function to
// insert Matrices
M.getMatrix();
// calling subtractMatrix() function to
// subtract Matrices
M.subtractMatrix();
return 0;
}
Output
Enter 1st Matrix elements :
[0][0]: 11
[0][1]: 12
[0][2]: 13
[1][0]: 14
[1][1]: 15
[1][2]: 16
[2][0]: 17
[2][1]: 18
[2][2]: 19
Enter 2nd Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 9
Entered 1st Matrix :
11 12 13
14 15 16
17 18 19
Entered 2nd Matrix :
1 2 3
4 5 6
7 8 9
Subtracted Matrix :
10 10 10
10 10 10
10 10 10
Explanation
In the above code, we have created a class Matrix, two int type 2d array data members matrix1[3][3] and matrix2[3][3] to store the matrix, and public member functions getMatrix() and subtractMatrix() to insert matrices and to subtract matrices.
In the main() function, we are creating an object M of class Matrix, reading matrices given by the user using getMatrix() function, and finally calling the subtractMatrix() member function to subtract the given matrices. The subtractMatrix() function contains the logic to subtract the given matrices and printing the result.
C++ Class and Object Programs (Set 2) »