Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- void FillArray(int **arrtwoDimArr, const int rows, const int cols);
- void ShawArray(int **arrtwoDimArr, const int rows, const int cols);
- void SwapMaxElofRowWithFirst(int **arrtwoDimArr, const int rows, const int cols);
- void DeleteDynamicArray(int **&arrtwoDimArr, const int rows, const int cols) {
- for (int i = 0; i < rows; i++) {
- delete[] arrtwoDimArr[i];
- }
- delete[]arrtwoDimArr;
- arrtwoDimArr = nullptr;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- srand(time(NULL));
- int rows, cols;
- std::cout << "Введите количество строк массива ";
- std::cin >> rows;
- std::cout << "Введите количество столбов массива ";
- std::cin >> cols;
- int **twoDimArr = new int *[rows];
- for (int i = 0; i < rows; i++) {
- twoDimArr[i] = new int[cols];
- }
- FillArray(twoDimArr, rows, cols);
- ShawArray(twoDimArr, rows, cols);
- std::cout << std::endl;
- SwapMaxElofRowWithFirst(twoDimArr, rows, cols);
- ShawArray(twoDimArr, rows, cols);
- DeleteDynamicArray(twoDimArr, rows, cols);
- return 0;
- }
- void FillArray(int **arrtwoDimArr, const int rows, const int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- arrtwoDimArr[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 (arrtwoDimArr[k][l] == arrtwoDimArr[i][j]) {
- arrtwoDimArr[i][j] = 1 + rand() % (cols*rows * 2);
- k = -1;
- break;
- }
- }
- }
- }
- }
- }
- void ShawArray(int **arrtwoDimArr, const int rows, const int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << arrtwoDimArr[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- void SwapMaxElofRowWithFirst(int **arrtwoDimArr, const int rows, const int cols) {
- int temp_1 = arrtwoDimArr[0][0], temp_2;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- for (int l = 0; l < cols; l++) {
- if (arrtwoDimArr[i][j] > arrtwoDimArr[i][l]) {
- if (temp_1 < arrtwoDimArr[i][j]) {
- temp_1 = arrtwoDimArr[i][j];
- }
- }
- }
- }
- for (int j = 0; j < cols; j++) {
- if (arrtwoDimArr[i][j] == temp_1) {
- temp_2 = arrtwoDimArr[i][0];
- arrtwoDimArr[i][0] = temp_1;
- arrtwoDimArr[i][j] = temp_2;
- }
- }
- if (i != rows - 1) {
- temp_1 = arrtwoDimArr[i + 1][0];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment