Vla_DOS

Завдання 3

Feb 20th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void RandArray(int** array, int row, int column)
  6. {
  7.     for (int i = 0; i < row; i++)
  8.     {
  9.         for (int j = 0; j < column; j++)
  10.         {
  11.             array[i][j] = 1 + rand() % 50;
  12.         }
  13.     }
  14. }
  15. void PrintArray(int** array, int row, int column)
  16. {
  17.     for (int i = 0; i < row; i++)
  18.     {
  19.         for (int j = 0; j < column; j++)
  20.         {
  21.             cout << array[i][j] << "\t";
  22.         }
  23.         cout << endl;
  24.     }
  25. }
  26.  
  27.  
  28.  
  29. void ExchangeSortArray(int** array, int row, int column)
  30. {
  31.     for (int i = 0; i < row; i++)
  32.     {
  33.         for (int j = 0; j < column; j++)
  34.         {
  35.             int m = i;
  36.             int n = j + 1;
  37.             while (true)
  38.             {
  39.                 if (n == column)
  40.                 {
  41.                     n = 0;
  42.                     m++;
  43.                     if (m == row)
  44.                         break;
  45.                 }
  46.                 if (array[i][j] > array[m][n])
  47.                     swap(array[i][j], array[m][n]);
  48.                 n++;
  49.             }
  50.         }
  51.     }
  52. }
  53. int main()
  54. {
  55.     int row, column;
  56.     cin>>row;
  57.     cin>>column;
  58.  
  59.     int** array = new int* [row];
  60.     for (int count = 0; count < row; count++)
  61.         array[count] = new int[column];
  62.     RandArray(array, row, column);
  63.     cout << "Init random array: " << endl;
  64.     PrintArray(array, row, column);
  65.     cout << endl;
  66.     cout << "After sort: " << endl;
  67.     ExchangeSortArray(array, row, column);
  68.     PrintArray(array, row, column);
  69.     return 0;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment