Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     setlocale(LC_ALL , "Russian");
  10.  
  11.     int n;
  12.     cout << "Введите количество строк: ";
  13.     cin >> n;
  14.     int ** a = new int * [n];
  15.     for (int i = 0; i < n; i++) {                   //Создается массив                        
  16.         a[i] = new int[n];
  17.     }
  18.    
  19.     srand(time(NULL));
  20.     for (int i = 0; i < n; i++) {                   //Массив заполняется случайными числами
  21.         for (int j = 0; j < n; j++) {
  22.             a[i][j] = rand() % n + 1;
  23.         }
  24.     }
  25.  
  26.     for (int i = 0; i < n; i++) {                   //Вывод массива на экран
  27.         for (int j = 0; j < n; j++) {
  28.             cout << a[i][j] << '\t';
  29.         }
  30.         cout << endl;
  31.     }
  32.     system("pause");
  33.     cout << endl;
  34.  
  35.     for (int g = 0; g < n; g++) {                   //Массив сортирует числа в каждой строке от большего к меньшему
  36.         for (int i = 0; i < n; i++) {
  37.             for (int j = 0; j < n-1; j++) {
  38.                 if (a[g][j] < a[g][j+1]) {
  39.                     swap(a[g][j], a[g][j+1]);
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     for (int i = 0; i < n; i++) {                   //Вывод массива на экран
  46.         for (int j = 0; j < n; j++) {
  47.             cout << a[i][j] << '\t';
  48.         }
  49.         cout << endl;
  50.     }
  51.     system("pause");
  52.     cout << endl;
  53.  
  54.     for (int g = 0; g < n; g++) {                   //Массив сортируется по первому столбцу от меньшего к большему
  55.         for (int i = 0; i < n-1; i++) {
  56.             if (a[i][0] > a[i+1][0]){
  57.                 swap(a[i], a[i+1]);
  58.             }
  59.         }
  60.     }
  61.  
  62.     for (int i = 0; i < n; i++) {                   //Вывод массива на экран
  63.         for (int j = 0; j < n; j++) {
  64.             cout << a[i][j] << '\t';
  65.         }
  66.         cout << endl;
  67.     }
  68.     system("pause");
  69.    
  70.     _getch();
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement