Website Summary Files Commits Branches @commit @diff @stats
path: root / Week01 / Exercise01
diff options
context:
space:
mode:
Commitfcf38e62e8160f2c9901bdfd33760afed28beb8d (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-02-22 19:18:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-02-22 19:18:46 +0200
Files838fd5897d88700491a389cdf6837e35da3347ae /Week01/Exercise01
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.tar
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.tar.gz
oop-2025-solutions-fcf38e62e8160f2c9901bdfd33760afed28beb8d.zip
Parentc365ede952f852c6e8feb92fc3a0766186f989f1 (diff)
feat(Week01): Add solutions
3 files changed, 68 insertions, 0 deletions
-rw-r--r--Week01/Exercise01/main.cpp29
-rw-r--r--Week01/Exercise01/matrix.cpp28
-rw-r--r--Week01/Exercise01/matrix.h11
diff --git a/Week01/Exercise01/main.cpp b/Week01/Exercise01/main.cpp
new file mode 100644
index 0000000..7958615
--- /dev/null
+++ b/Week01/Exercise01/main.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+#include "matrix.h"
+
+int main() {
+ unsigned rowsA, colsA;
+ std::cin >> rowsA >> colsA;
+
+ int** matA = newMatrix(rowsA, colsA);
+
+ for (unsigned row = 0; row < rowsA; ++row)
+ for (unsigned col = 0; col < colsA; ++col)
+ std::cin >> matA[row][col];
+
+ unsigned rowsB, colsB;
+ std::cin >> rowsB >> colsB;
+
+ int** matB = newMatrix(rowsB, colsB);
+
+ for (unsigned row = 0; row < rowsB; ++row)
+ for (unsigned col = 0; col < colsB; ++col)
+ std::cin >> matB[row][col];
+
+ std::cout << (sumMatrix((const int**)matA, rowsA, colsA) - sumMatrix((const int**)matB, rowsB, colsB)) << std::endl;
+
+ deleteMatrix(matA, rowsA);
+ deleteMatrix(matB, rowsB);
+
+ return 0;
+}
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;
+}
diff --git a/Week01/Exercise01/matrix.h b/Week01/Exercise01/matrix.h
new file mode 100644
index 0000000..334fab0
--- /dev/null
+++ b/Week01/Exercise01/matrix.h
@@ -0,0 +1,11 @@
+#ifndef _MATRIX
+#define _MATRIX
+
+// Създава нова динамично-заделена матрица
+int** newMatrix(unsigned rows, unsigned cols);
+// Изтрива динамично-заделена матрица
+void deleteMatrix(int**& matrix, unsigned rows); // Запишете nullptr в matrix!
+// Връща сумата на всички елементи в матрицата
+int sumMatrix(const int** matrix, unsigned rows, unsigned cols);
+
+#endif