Website Summary Files Commits Branches @commit @diff @stats
path: root / Week08 / Exercise06.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/Exercise06.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, 40 insertions, 0 deletions
-rw-r--r--Week08/Exercise06.cpp40
diff --git a/Week08/Exercise06.cpp b/Week08/Exercise06.cpp
new file mode 100644
index 0000000..30ef172
--- /dev/null
+++ b/Week08/Exercise06.cpp
@@ -0,0 +1,40 @@
+#include <iostream>
+
+void swap(double &a, double &b) {
+ double temp = a;
+ a = b;
+ b = temp;
+}
+
+void swapColumns(double* mat, unsigned int rows, unsigned int cols, unsigned int column1, unsigned int column2) {
+ if (column1 >= cols || column2 >= cols)
+ return;
+
+ for (unsigned int row = 0; row < rows; row++)
+ /* За повече информация защо така индексираме в матриците, вижте
+ * https://syndamia.com/teaching/upp-2025/cpp-pitfalls/#-int73-vs-int3-vs-int
+ */
+ swap(mat[row * cols + column1], mat[row * cols + column2]);
+}
+
+int main() {
+ const unsigned int ROWS = 3, COLS = 5;
+ double mat[ROWS][COLS];
+
+ for (unsigned int i = 0; i < ROWS; i++)
+ for (unsigned int j = 0; j < COLS; j++)
+ std::cin >> mat[i][j];
+
+ unsigned int col1, col2;
+ std::cin >> col1 >> col2;
+
+ swapColumns((double*)mat, ROWS, COLS, col1, col2);
+
+ for (unsigned int row = 0; row < ROWS; row++) {
+ for (unsigned int col = 0; col < COLS; col++)
+ std::cout << mat[row][col] << ' ';
+ std::cout << std::endl;
+ }
+
+ return 0;
+}