c3
This commit is contained in:
parent
4744ebb565
commit
deba2fdcf0
5 changed files with 160 additions and 8 deletions
41
1sem/programming basics/z11/31.cpp
Normal file
41
1sem/programming basics/z11/31.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
51
1sem/programming basics/z11/32.cpp
Normal file
51
1sem/programming basics/z11/32.cpp
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
53
1sem/programming basics/z11/33.cpp
Normal file
53
1sem/programming basics/z11/33.cpp
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue