Advertisement
35657

Untitled

Nov 30th, 2023
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1.  
  2. #include<stdlib.h> // в этом файле содержатся rand и srand
  3. #include<time.h> // в этом файле содержится функция time
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11.     srand(time(NULL));
  12.  
  13.     setlocale(LC_ALL, "ru");
  14.  
  15.     const int row = 5;
  16.     const int col = 6;
  17.  
  18.     int arr[row][col];
  19.  
  20.     int type, count;
  21.  
  22.     cout << "Введите количество разрядов сдвига: ";
  23.  
  24.     cin >> count;
  25.  
  26.     cout << "Введите 1 для сдвига вправо, 2 для сдвига влево, 3 для сдвига вниз, 4 для сдвига вверх: ";
  27.  
  28.     cin >> type;
  29.  
  30.     for (int i = 0; i < row; i++) {
  31.         for (int j = 0; j < col; j++) {
  32.             arr[i][j] = rand() % 100;
  33.             cout << arr[i][j] << "\t";
  34.         }
  35.         cout << endl;
  36.     }
  37.     cout << endl;
  38.  
  39.  
  40.     if (type == 1) { // сдвиг массива вправо
  41.         for (int n = 0; n < count % col; n++) {
  42.             for (int i = 0; i < row; i++) {
  43.                 int temp = arr[i][col - 1];
  44.                 for (int j = col - 1; j > 0; j--) {
  45.                     arr[i][j] = arr[i][j - 1];
  46.                 }
  47.                 arr[i][0] = temp;
  48.             }
  49.         }
  50.     }
  51.     else if (type == 2) { // сдвиг массива влево
  52.         for (int n = 0; n < count % col; n++) {
  53.             for (int i = 0; i < row; i++) {
  54.                 int temp = arr[i][0];
  55.                 for (int j = 0; j < col - 1; j++) {
  56.                     arr[i][j] = arr[i][j + 1];
  57.                 }
  58.                 arr[i][col - 1] = temp;
  59.             }
  60.         }
  61.     }
  62.     else if (type == 3) { // сдвиг массива вниз
  63.         for (int n = 0; n < count % row; n++) {
  64.             for (int i = 0; i < col; i++) {
  65.                 int temp = arr[row - 1][i];
  66.                 for (int j = row - 1; j > 0; j--) {
  67.                     arr[j][i] = arr[j - 1][i];
  68.                 }
  69.                 arr[0][i] = temp;
  70.             }
  71.         }
  72.     }
  73.    
  74.  
  75.     for (int i = 0; i < row; i++) {
  76.         for (int j = 0; j < col; j++) {
  77.             cout << arr[i][j] << "\t";
  78.         }
  79.         cout << endl;
  80.     }
  81.     cout << endl;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement