#include <iostream>
#include <vector>
using namespace std;
void swapRowsAndColumns(vector<vector<int>>& matrix, int x, int y) {
int n = matrix.size();
if (x < 1 || x > n || y < 1 || y > n) {
cout << "Invalid row or column indices." << endl;
return;
}
// Swap rows
swap(matrix[x - 1], matrix[y - 1]);
// Swap columns
for (int i = 0; i < n; i++) {
swap(matrix[i][x - 1], matrix[i][y - 1]);
}
}
void printMatrix(const vector<vector<int>>& matrix) {
for (const vector<int>& row : matrix) {
for (int value : row) {
cout << value << " ";
}
cout << endl;
}
}
int main() {
int n, x, y;
cin >> n >> x >> y;
vector<vector<int>> matrix(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
// cout << "Original Matrix:" << endl;
// printMatrix(matrix);
swapRowsAndColumns(matrix, x, y);
//cout << "Matrix After Swapping Rows and Columns:" << endl;
printMatrix(matrix);
return 0;
}
Sunday, October 15, 2023
2D Matrix C++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment