Website Summary Files Commits Branches @commit @diff @stats
path: root / Week08 / Exercise06.cpp
blob: 30ef172c2fb41a2a0f6cfeaf0c509e4db8eb6f31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}