-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
64 lines (58 loc) · 1.97 KB
/
Matrix.cpp
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
#include "Matrix.h"
//Constructor
Matrix::Matrix(unsigned int row, unsigned int column) :
num_of_rows(row), num_of_columns(column)
{ data.resize(row, std::vector<int>(column, 0)); } //Initialize 2d vector
//Getters
unsigned int Matrix::get_num_of_rows() const { return num_of_rows; }
unsigned int Matrix::get_num_of_columns() const { return num_of_columns; }
//Class member functions
/** Gets value from data based on user inputed indexes
@param new_i: row index
@param new_j: column index
@returns: the value at new_i and new_j index
*/
int Matrix::get(unsigned int new_i, unsigned int new_j) const {
return data[new_i][new_j]; }
/** Set the value at user inputed indexes
@param new_i: row index
@param new_j: column index
@param new_val: integer value to go in data 2d vector
*/
void Matrix::set(unsigned int new_i, unsigned int new_j, int new_val) { data[new_i][new_j] = new_val; }
//Operators
/** Overloads "Matrix + Matrix"
@param other: the second operand
@returns: the addition result
*/
Matrix Matrix::operator + (const Matrix& other) const {
Matrix result(other.num_of_rows, other.num_of_columns);
for (unsigned int i = 0; i < result.num_of_rows; i++) {
for (unsigned int j = 0; j < result.num_of_columns; j++) {
int this_matrix = get(i, j);
int other_matrix = other.get(i, j);
int value = this_matrix + other_matrix;
result.set(i, j, value); }
}
return result; }
/** Overloads << operator
@param out: output stream
@param m: a matrix to be added
@returns: the output stream
*/
std::ostream& operator << (std::ostream& out, const Matrix& m) {
unsigned int rows = m.num_of_rows;
unsigned int columns = m.num_of_columns;
int value;
out << rows << "\t" << columns << "\n\n";
for (unsigned int i = 0; i < rows; i++) {
for (unsigned int j = 0; j < columns; j++) {
value = m.get(i, j);
out << value;
if (j == columns - 1) {
out << std::endl; }
else {
out << " "; }
}
}
return out; }