tranerius

18. Свап наибольшего и первого элемента динам двумер массива

Dec 13th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. void FillArray(int **arrtwoDimArr, const int rows, const int cols);
  4. void ShawArray(int **arrtwoDimArr, const int rows, const int cols);
  5. void SwapMaxElofRowWithFirst(int **arrtwoDimArr, const int rows, const int cols);
  6. void DeleteDynamicArray(int **&arrtwoDimArr, const int rows, const int cols) {
  7.     for (int i = 0; i < rows; i++) {
  8.         delete[] arrtwoDimArr[i];
  9.     }
  10.     delete[]arrtwoDimArr;
  11.     arrtwoDimArr = nullptr;
  12. }
  13. int main() {
  14.     setlocale(LC_ALL, "ru");
  15.     srand(time(NULL));
  16.     int rows, cols;
  17.     std::cout << "Введите количество строк массива ";
  18.     std::cin >> rows;
  19.     std::cout << "Введите количество столбов массива ";
  20.     std::cin >> cols;
  21.     int **twoDimArr = new int *[rows];
  22.     for (int i = 0; i < rows; i++) {
  23.         twoDimArr[i] = new int[cols];
  24.     }
  25.     FillArray(twoDimArr, rows, cols);
  26.     ShawArray(twoDimArr, rows, cols);
  27.     std::cout << std::endl;
  28.     SwapMaxElofRowWithFirst(twoDimArr, rows, cols);
  29.     ShawArray(twoDimArr, rows, cols);
  30.     DeleteDynamicArray(twoDimArr, rows, cols);
  31.     return 0;
  32. }
  33. void FillArray(int **arrtwoDimArr, const int rows, const int cols) {
  34.     for (int i = 0; i < rows; i++) {
  35.         for (int j = 0; j < cols; j++) {
  36.             arrtwoDimArr[i][j] = 1 + rand() % (cols*rows * 2);
  37.         }
  38.     }
  39.     for (int i = 0; i < rows; i++) {
  40.         for (int j = 0; j < cols; j++) {
  41.             for (int k = 0; k < rows; k++) {
  42.                 for (int l = 0; l < cols; l++) {
  43.                     if (i == k && j == l) {
  44.                         continue;
  45.                     }
  46.                     if (arrtwoDimArr[k][l] == arrtwoDimArr[i][j]) {
  47.                         arrtwoDimArr[i][j] = 1 + rand() % (cols*rows * 2);
  48.                         k = -1;
  49.                         break;
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
  56. void ShawArray(int **arrtwoDimArr, const int rows, const int cols) {
  57.     for (int i = 0; i < rows; i++) {
  58.         for (int j = 0; j < cols; j++) {
  59.             std::cout << arrtwoDimArr[i][j] << "\t";
  60.         }
  61.         std::cout << std::endl;
  62.     }
  63. }
  64. void SwapMaxElofRowWithFirst(int **arrtwoDimArr, const int rows, const int cols) {
  65.     int temp_1 = arrtwoDimArr[0][0], temp_2;
  66.     for (int i = 0; i < rows; i++) {
  67.         for (int j = 0; j < cols; j++) {
  68.             for (int l = 0; l < cols; l++) {
  69.                 if (arrtwoDimArr[i][j] > arrtwoDimArr[i][l]) {
  70.                     if (temp_1 < arrtwoDimArr[i][j]) {
  71.                         temp_1 = arrtwoDimArr[i][j];
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         for (int j = 0; j < cols; j++) {
  77.             if (arrtwoDimArr[i][j] == temp_1) {
  78.                 temp_2 = arrtwoDimArr[i][0];
  79.                 arrtwoDimArr[i][0] = temp_1;
  80.                 arrtwoDimArr[i][j] = temp_2;
  81.             }
  82.         }
  83.         if (i != rows - 1) {
  84.             temp_1 = arrtwoDimArr[i + 1][0];
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment