forked from HemangTheHuman/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_subtraction.cpp
More file actions
56 lines (56 loc) · 1.47 KB
/
Matrix_subtraction.cpp
File metadata and controls
56 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>
using namespace std;
class matrixSubtraction{
int m,n;
vector<vector<long long >> matrix1,matrix2;
public:
matrixSubtraction(int m,int n){
this->m=m;
this->n=n;
matrix1.resize(m,vector<long long>(n));
matrix2.resize(m,vector<long long>(n));
inputMatrix(matrix1);
inputMatrix(matrix2);
}
void inputMatrix(vector<vector<long long>> &matrix){
cout<<"Input the matrix elements \n";
for(int i=0;i<m;i++){
cout<<"Input "<<n<<" elements of row "<<i+1<<" :";
for(int j=0;j<n;j++){
cin>>matrix[i][j];
}
}
}
void subtractMatrixAndDisplay(){
cout<<"\nFirst Matrix :\n\n";
displayMatrix(matrix1);
cout<<"Second Matrix :\n\n";
displayMatrix(matrix2);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
matrix1[i][j]-=matrix2[i][j];
}
}
cout<<"First Matrix - Second Matrix :"<<endl;
displayMatrix(matrix1);
}
void displayMatrix(vector<vector<long long>> &matrix){
for(auto & row:matrix){
for(auto &element:row){
cout<<element<<" ";
}
cout<<endl;
}
}
};
int main(){
int m,n;
cout<<"Input number of rows of the matrix : ";
cin>>m;
cout<<"Input number of columns of the matrix: ";
cin>>n;
matrixSubtraction solution(m,n);
solution.subtractMatrixAndDisplay();
return 0;
}