Website Summary Files Commits Branches @commit @diff @stats
path: root / Week01 / Exercise01 / matrix.cpp
diff options
context:
space:
mode:
1 files changed, 28 insertions, 0 deletions
-rw-r--r--Week01/Exercise01/matrix.cpp28
diff --git a/Week01/Exercise01/matrix.cpp b/Week01/Exercise01/matrix.cpp
new file mode 100644
index 0000000..c8bdff2
--- /dev/null
+++ b/Week01/Exercise01/matrix.cpp
@@ -0,0 +1,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;
+}