Website Summary Files Commits Branches @commit @diff @stats
path: root / Week01 / Exercise01 / matrix.cpp
blob: c8bdff2fd45328adaadcc0675022eca016bfccb7 (plain) (blame)
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
#include "matrix.h"

int** newMatrix(unsigned rows, unsigned cols) {
	int** mat = new int*[rows];

	for (unsigned i = 0; i < rows; ++i)
		mat[i] = new int[cols];

	return mat;
}

void deleteMatrix(int**& matrix, unsigned rows) {
	for (unsigned i = 0; i < rows; ++i)
		delete[] matrix[i];
	delete[] matrix;

	matrix = nullptr;
}

int sumMatrix(const int** matrix, unsigned rows, unsigned cols) {
	int sum = 0;

	for (unsigned row = 0; row < rows; ++row)
		for (unsigned col = 0; col < cols; ++col)
			sum += matrix[row][col];

	return sum;
}