-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
43 lines (31 loc) · 871 Bytes
/
matrix.cpp
File metadata and controls
43 lines (31 loc) · 871 Bytes
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
#include "matrix.h"
Matrix::Matrix(int rows, int cols) : rows(rows), cols(cols) {
data.resize(rows, std::vector<int>(cols, 0));
}
int Matrix::getRows() const {
return rows;
}
int Matrix::getCols() const {
return cols;
}
int Matrix::get(int row, int col) const {
if (row >= 0 && row < rows && col >= 0 && col < cols) {
return data[row][col];
}
throw std::out_of_range("Invalid row or column index");
}
void Matrix::set(int row, int col, int value) {
if (row >= 0 && row < rows && col >= 0 && col < cols) {
data[row][col] = value;
} else {
throw std::out_of_range("Invalid row or column index");
}
}
void Matrix::print() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}