From 5294e73a21f3402e14fcf3379c721275e2dd19c2 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 23 Nov 2025 15:25:29 +0200 Subject: feat(Week08): Add solutions --- Week08/Exercise06.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Week08/Exercise06.cpp (limited to 'Week08/Exercise06.cpp') 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 + +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; +} -- cgit v1.3.1-23-gd82d