-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix-addition.cpp
More file actions
52 lines (42 loc) · 1.37 KB
/
Copy pathmatrix-addition.cpp
File metadata and controls
52 lines (42 loc) · 1.37 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
#include <vector>
#include <iostream>
std::vector<std::vector<int> > matrixAddition(std::vector<std::vector<int> > a,std::vector<std::vector<int> > b){
int a_rows = a.size();
int a_cols = a[0].size();
std::vector<std::vector<int>> matrix_c(a_rows,std::vector<int>(a_cols));
for(int row = 0; row < a_rows; row++){
for(int col = 0; col < a_cols; col++){
matrix_c[row][col] = a[row][col] + b[row][col];
}
}
return matrix_c;
}
/** Testing and Main functions
** Not part of solution
**/
void init_matrix(std::vector<std::vector<int>> &matrix) {
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
matrix[i][j] = i + 1 * 2; //initialize test values
}
}
}
void print_matrix(std::vector<std::vector<int>> matrix) {
for(const auto &row : matrix) {
for(int num : row) {
std::cout << num << " ";
}
std::cout << "\n\n";
}
}
int main() {
//use vector's constructor to construct a vector inside another vector
std::vector<std::vector<int>> matrix_a(2, std::vector<int>(3));
std::vector<std::vector<int>> matrix_b(2, std::vector<int>(3));
init_matrix(matrix_a);
init_matrix(matrix_b);
print_matrix(matrix_a);
print_matrix(matrix_b);
print_matrix(matrixAddition(matrix_a,matrix_b));
return 0;
}