-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_class_simple.cpp
More file actions
61 lines (53 loc) · 1.59 KB
/
Copy pathmatrix_class_simple.cpp
File metadata and controls
61 lines (53 loc) · 1.59 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
#include <iostream>
#include <string>
class Matrix {
private:
float* data;
public:
int height;
int width;
Matrix(int height_inp, int width_inp) {
height = height_inp;
width = width_inp;
int data_size = height * width;
data = new float[data_size];
}
void set_value (int x, int y, float value) {
data[y * width + x] = value;
}
Matrix operator + (const Matrix &matrix_right) {
Matrix matrix_result(height, width);
int offset = 0;
for(int row_count = 0; row_count < height; row_count++){
for(int column_count = 0; column_count < width; column_count++){
matrix_result.data[offset] = data[offset] + matrix_right.data[offset];
offset++;
}
}
return matrix_result;
}
void print() {
int offset = 0;
for(int row_count = 0; row_count < height; row_count++){
std::cout << '|';
for(int column_count = 0; column_count < width; column_count++){
std::cout << data[offset];
if(column_count < (width - 1)){
std::cout << "\t";
}
offset++;
}
std::cout << "|\n";
}
}
};
int main()
{
Matrix A_matrix(3, 4);
Matrix B_matrix(3, 4);
Matrix C_matrix(3, 4);
A_matrix.set_value(1, 2, 1.2f);
B_matrix.set_value(2, 2, 2.2f);
C_matrix = A_matrix + B_matrix;
C_matrix.print();
}