Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- class twoDimArr {
- int **arr, rows, cols;
- public:
- twoDimArr(const int rows, const int cols) {
- this->rows = rows;
- this->cols = cols;
- this->arr = new int*[rows];
- for (int i = 0; i < rows; i++) {
- arr[i] = new int[cols];
- }
- }
- void FillArray() {
- char answer;
- do {
- std::cout << "Указать элементы матрицы вручную?(y/n) ";
- std::cin >> answer;
- if (answer != 'y' && answer != 'n') {
- std::cout << "Неверный ответ." << std::endl;
- }
- } while (answer != 'y' && answer != 'n');
- if (answer == 'y') {
- while (true) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << "Введите элемент " << i + 1 << ", " << j + 1 << " матрицы ";
- std::cin >> arr[i][j];
- }
- }
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << arr[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- do {
- std::cout << "Матрица составлена верно?(y/n) ";
- std::cin >> answer;
- if (answer != 'y' && answer != 'n') {
- std::cout << "Неверный ответ." << std::endl;
- }
- } while (answer != 'y' && answer != 'n');
- if (answer == 'y') {
- break;
- }
- else {
- continue;
- }
- }
- }
- else if (answer == 'n') {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- arr[i][j] = 1 + rand() % (cols*rows * 2);
- }
- }
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- for (int k = 0; k < rows; k++) {
- for (int l = 0; l < cols; l++) {
- if (i == k && j == l) {
- continue;
- }
- if (arr[i][j] == arr[k][l]) {
- arr[i][j] = 1 + rand() % (cols*rows * 2);
- k = -1;
- i = 0;
- j = 0;
- break;
- }
- }
- }
- }
- }
- }
- }
- void ShawArray() {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << arr[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- void Transpose() {
- int **transposeArr = new int*[cols];
- for (int i = 0; i < cols; i++) {
- transposeArr[i] = new int[rows];
- }
- for (int i = 0; i < cols; i++) {
- for (int j = 0; j < rows; j++) {
- transposeArr[i][j] = arr[j][i];
- }
- }
- for (int i = 0; i < rows; i++) {
- delete[]arr[i];
- }
- delete[]arr;
- this->arr = transposeArr;
- int temp = rows;
- this->rows = cols;
- this->cols = temp;
- }
- twoDimArr(const twoDimArr &object) {
- this->cols = object.cols;
- this->rows = object.rows;
- this->arr = new int*[object.rows];
- for (int i = 0; i < object.rows; i++) {
- arr[i] = new int[object.cols];
- }
- }
- ~twoDimArr() {
- for (int i = 0; i < rows; i++) {
- delete[]arr[i];
- }
- delete[]arr;
- arr = nullptr;
- }
- };
- int main() {
- setlocale(LC_ALL, "ru");
- srand(time(NULL));
- int rows, cols;
- std::cout << "Введите количество строк: ";
- std::cin >> rows;
- std::cout << "Введите количество столбцов: ";
- std::cin >> cols;
- twoDimArr a(rows, cols);
- twoDimArr b(a);
- a.FillArray();
- a.ShawArray();
- a.Transpose();
- std::cout << "Транспонированная матрица: " << std::endl;
- a.ShawArray();
- b.FillArray();
- std::cout << "Матрица 1:" << std::endl;
- a.ShawArray();
- std::cout << "Матрица 2:" << std::endl;
- b.ShawArray();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment