-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWork with matrices
105 lines (87 loc) · 2.13 KB
/
Work with matrices
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int mat1[2][2], mat2[3][3];
// Task 1 and Task 2
cout << "Input first matrix:" << endl;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
cin >> mat1[i][j];
}
}
cout << endl << "Input second matrix:" << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
cin >> mat2[i][j];
}
}
// Task 3
cout << endl << "First matrix:" << endl;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
cout << mat1[i][j] << " ";
}
cout << endl;
}
cout << endl << "Second matrix:" << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
cout << mat2[i][j] << " ";
}
cout << endl;
}
// Task 4 and input for Task 5
cout << "Input size of matrices: ";
vector <vector <int>> matrix1, matrix2, matrix_sum, matrix_sub;
int size, num; cin >> size;
cout << endl << "Input first matrix:" << endl;
for (int i = 0; i < size; i++){
vector <int> temp;
for (int j = 0; j < size; j++){
cin >> num;
temp.push_back(num);
}
matrix1.push_back(temp);
}
cout << endl << "Input second matrix:" << endl;
for (int i = 0; i < size; i++){
vector <int> temp;
for (int j = 0; j < size; j++){
cin >> num;
temp.push_back(num);
}
matrix2.push_back(temp);
}
for (int i = 0; i < size; i++){
vector <int> temp;
for (int j = 0; j < size; j++){
temp.push_back(matrix1[i][j] + matrix2[i][j]);
}
matrix_sum.push_back(temp);
}
cout << endl << "Sum matrix:" << endl;
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
cout << matrix_sum[i][j] << " ";
}
cout << endl;
}
// Task 5
for (int i = 0; i < size; i++){
vector <int> temp;
for (int j = 0; j < size; j++){
temp.push_back(matrix1[i][j] - matrix2[i][j]);
}
matrix_sub.push_back(temp);
}
cout << endl << "Sub matrix:" << endl;
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
cout << matrix_sub[i][j] << " ";
}
cout << endl;
}
return 0;
}