This commit is contained in:
Andrew 2019-11-19 00:19:04 +07:00
parent 4744ebb565
commit deba2fdcf0
5 changed files with 160 additions and 8 deletions

View file

@ -0,0 +1,41 @@
// pb_z11_31.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
int **read_matrix(size_t rows, size_t cols);
void print_matrix(int **matrix, size_t rows, size_t cols);
int **make2d(size_t nrows, size_t ncols, int val);
int main() {
size_t rows, cols;
int filler;
cout << "rows, cols, filling_val >>>";
cin >> rows >> cols >> filler;
int **matrix = make2d(rows, cols, filler);
print_matrix(matrix, rows, cols);
return 0;
}
int **make2d(size_t nrows, size_t ncols, int val) {
int **out = new int*[nrows];
for(size_t i = 0; i < nrows; i++) {
int *row = new int[ncols];
for(size_t j = 0; j < ncols; j++) {
*(row + j) = val;
}
*(out + i) = row;
}
return out;
}
void print_matrix(int **matrix, size_t rows, size_t cols) {
for(size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < cols; j++) {
cout << *(*(matrix + i) + j) << " ";
}
cout << endl;
}
}

View file

@ -0,0 +1,51 @@
// pb_z11_32.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
int **read_matrix(size_t nrows, size_t ncols);
void print_matrix(int **matrix, size_t nrows, size_t ncols);
void fliplr(int **matrix, size_t nrows, size_t ncols);
int main() {
size_t rows, cols;
cout << "rows, cols >>>";
cin >> rows >> cols;
int **matrix = read_matrix(rows, cols);
cout << "Before:" << endl;
print_matrix(matrix, rows, cols);
fliplr(matrix, rows, cols);
cout << "After:" << endl;
print_matrix(matrix, rows, cols);
return 0;
}
int **read_matrix(size_t nrows, size_t ncols) {
int **out = new int*[nrows];
for(size_t i = 0; i < nrows; i++) {
int *row = new int[ncols];
for(size_t j = 0; j < ncols; j++) {
cin >> *(row + j);
}
*(out + i) = row;
}
return out;
}
void print_matrix(int **matrix, size_t nrows, size_t ncols) {
for(size_t i = 0; i < nrows; i++) {
for(size_t j = 0; j < ncols; j++) {
cout << *(*(matrix + i) + j) << " ";
}
cout << endl;
}
}
void fliplr(int **matrix, size_t nrows, size_t ncols) {
for(size_t i = 0; i < nrows; i++) {
for(size_t j = 0; j < ncols/2; j++) {
swap(*(*(matrix + i) + j), *(*(matrix + i) + ncols - j - 1));
}
}
}

View file

@ -0,0 +1,53 @@
// pb_z11_33.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
int **read_matrix(size_t nrows, size_t ncols);
void print_matrix(int **matrix, size_t nrows, size_t ncols);
void transpose(int **matrix, size_t size);
int main() {
size_t size;
cout << "size >>>";
cin >> size;
int **matrix = read_matrix(size, size);
cout << "Before:" << endl;
print_matrix(matrix, size, size);
transpose(matrix, size);
cout << "After:" << endl;
print_matrix(matrix, size, size);
return 0;
}
int **read_matrix(size_t nrows, size_t ncols) {
int **out = new int*[nrows];
for(size_t i = 0; i < nrows; i++) {
int *row = new int[ncols];
for(size_t j = 0; j < ncols; j++) {
cin >> *(row + j);
}
*(out + i) = row;
}
return out;
}
void print_matrix(int **matrix, size_t nrows, size_t ncols) {
for(size_t i = 0; i < nrows; i++) {
for(size_t j = 0; j < ncols; j++) {
cout << *(*(matrix + i) + j) << " ";
}
cout << endl;
}
}
void transpose(int **matrix, size_t size) {
for(size_t i = 0; i < size; i++) {
for(size_t j = i; j < size; j++) {
int ri = *(*(matrix + i) + j);
int ci = *(*(matrix + j) + i);
swap(*(*(matrix + i) + j), *(*(matrix + j) + i));
}
}
}