Website Summary Files Commits Branches @commit @diff @stats
path: root / Week08 / Exercise11.cpp
diff options
context:
space:
mode:
Commit5294e73a21f3402e14fcf3379c721275e2dd19c2 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-11-23 15:25:29 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-11-23 16:05:21 +0200
Files3f02cc12e0868ac043d7f937fec380b29f54c71a /Week08/Exercise11.cpp
upp-2025-solutions-5294e73a21f3402e14fcf3379c721275e2dd19c2.tar
upp-2025-solutions-5294e73a21f3402e14fcf3379c721275e2dd19c2.tar.gz
upp-2025-solutions-5294e73a21f3402e14fcf3379c721275e2dd19c2.zip
Parent3e8ac7ba4a8f410703aa5a8426f542217adcbf2f (diff)
feat(Week08): Add solutions
1 files changed, 65 insertions, 0 deletions
-rw-r--r--Week08/Exercise11.cpp65
diff --git a/Week08/Exercise11.cpp b/Week08/Exercise11.cpp
new file mode 100644
index 0000000..a2922bb
--- /dev/null
+++ b/Week08/Exercise11.cpp
@@ -0,0 +1,65 @@
+#include <iostream>
+
+/* В това решение използваме int за размери, вместо unsigned int, защото
+ * правим изваждания, които се обработват по-лесно като можеш да имаш отрицателни числа.
+ * Може да се преработи с unsigned int (и окуражавам да го направите), но кода става
+ * по-досаден и грозен.
+ */
+const int SIZE = 10;
+
+int clamp(int value, int min, int max) {
+ if (value < min) return min;
+ if (value > max) return max;
+ return value;
+}
+
+void sumCells(const unsigned int mat[SIZE][SIZE],
+ int startRow, int endRow,
+ int startCol, int endCol,
+ unsigned int &sum, unsigned int &count)
+{
+ startRow = clamp(startRow, 0, SIZE - 1);
+ endRow = clamp(endRow, 0, SIZE - 1);
+ startCol = clamp(startCol, 0, SIZE - 1);
+ endCol = clamp(endCol, 0, SIZE - 1);
+
+ for (int row = startRow; row <= endRow; row++) {
+ for (int col = startCol; col <= endCol; col++) {
+ sum += mat[row][col];
+ count++;
+ }
+ }
+}
+
+double averageCells(const unsigned int mat[SIZE][SIZE], int row, int col, unsigned int radius) {
+ unsigned int sum = 0, sumCount = 0;
+
+ sumCells(mat, row - radius, row + radius, col - radius, col + radius, sum, sumCount);
+
+ return (double)sum / sumCount;
+}
+
+int main() {
+ unsigned int mat[SIZE][SIZE];
+
+ for (int i = 0; i < SIZE; i++)
+ for (int j = 0; j < SIZE; j++)
+ std::cin >> mat[i][j];
+
+ unsigned int radius;
+ std::cin >> radius;
+
+ double blurred[SIZE][SIZE] = { 0 };
+
+ for (int i = 0; i < SIZE; i++)
+ for (int j = 0; j < SIZE; j++)
+ blurred[i][j] = averageCells(mat, i, j, radius);
+
+ for (int i = 0; i < SIZE; i++) {
+ for (int j = 0; j < SIZE; j++)
+ std::cout << blurred[i][j] << ' ';
+ std::cout << std::endl;
+ }
+
+ return 0;
+}