Repository navigation
Expand file tree
/
Copy path006_matrix.cpp
More file actions
71 lines (60 loc) · 1.51 KB
/
Copy path006_matrix.cpp
File metadata and controls
71 lines (60 loc) · 1.51 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
class Matrix {
int a[10][10], r, c;
public:
void read(){
cout << "Enter rows and columns: ";
cin >> r >> c;
cout << "Enter elements:" << endl;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
cin >> a[i][j];
}
void print(){
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
void add(Matrix b){
cout << "Addition:" << endl;
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++)
cout << a[i][j] + b.a[i][j] << " ";
cout << endl;
}
}
void subt(Matrix b){
cout << "Subtraction:" << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
cout << a[i][j] - b.a[i][j] << " ";
cout << endl;
}
}
void multi(Matrix b){
cout << "Multiplication:" << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < b.c; j++){
int sum = 0;
for (int k = 0; k < c; k++)
sum += a[i][k] * b.a[k][j];
cout << sum << " ";
}
cout << endl;
}
}
};
int main(){
Matrix A, B;
cout << "First matrix:" << endl;
A.read();
cout << "Second matrix:" << endl;
B.read();
A.add(B);
A.subt(B);
A.multi(B);
return 0;
}