Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise03.cpp
blob: 5815b58de96f4a719284a97b825255faedf3c5c7 (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
#include <iostream>

int main() {
	unsigned int rows, cols;
	std::cin >> rows >> cols;

	unsigned int** mat = new unsigned int*[rows];
	for (unsigned int row = 0; row < rows; row++) {
		mat[row] = new unsigned int[cols];

		for (unsigned int col = 0; col < cols; col++)
			std::cin >> mat[row][col];
	}

	/* По принцип можем да решим задачата, все едно е динамична памет,
	 * тоест да разменяме първи елемент от първи ред с първи от последен
	 * ред, втори от първи ред с втори от последен ред и так. нат.
	 *
	 * Но тук, по-простия начин е да разменим указатели, което също е по-ефикасно.
	 */
	for (unsigned int row = 0; row < rows / 2; row++) {
		unsigned int* temp = mat[row];
		mat[row] = mat[rows - 1 - row];
		mat[rows - 1 - row] = temp;
	}

	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;
	}

	for (unsigned int row = 0; row < rows; row++)
		delete[] mat[row];
	delete[] mat;

	return 0;
}