Advertisement
malixds_

inf12_20

Dec 8th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main() {
  6.     const int size = 3;
  7.     cout.setf(ios::fixed);
  8.     cout.precision(0);
  9.     srand(time(NULL));
  10.     setlocale(LC_ALL, "ru");
  11.     int i, j, temp;
  12.     const int ROW = 6;
  13.     const int COL = 6;
  14.     float a[ROW][COL];
  15.     float b[3][6];
  16.     float c[size][size];
  17.     int ans;
  18.     cout << "Нажмите 1, если Вы хотите заполнить матрицу самостоятельно" <<
  19.         " и 0, если требуется автоматическое её заполнение:";
  20.  
  21.     cin >> ans;
  22.     if (ans == 1) {
  23.         for (int i = 0; i < ROW; i++) {
  24.             for (int j = 0; j < COL; j++) {
  25.                 cin >> a[i][j];
  26.             }
  27.         }
  28.         for (int i = 0; i < ROW; i++) {
  29.             cout << endl;
  30.             for (int j = 0; j < COL; j++) {
  31.                 cout << a[i][j] << " ";
  32.             }
  33.         }
  34.     }
  35.     else if (ans == 0) {
  36.         for (int i = 0; i < ROW; i++) {
  37.             int randNum = rand() % 100;
  38.             for (int j = 0; j < COL; j++) {
  39.                 a[i][j] = rand() % 100;
  40.             }
  41.         }
  42.         for (int i = 0; i < ROW; i++) {
  43.             cout << endl;
  44.             for (int j = 0; j < COL; j++) {
  45.                 cout << a[i][j] << " ";
  46.             }
  47.         }
  48.     }
  49.     cout << endl;
  50.     cout << endl;
  51.     cout << endl;
  52.  
  53.     for (int j = 0; j < 6; j++) {
  54.         b[0][j] = a[0][j] / a[1][j];
  55.         if (a[1][j] == 0) {
  56.             b[0][j] = 0;
  57.         }
  58.     }
  59.     for (int j = 0; j < 6; j++) {
  60.         b[1][j] = a[2][j] / a[3][j];
  61.         if (a[3][j] == 0) {
  62.             b[1][j] = 0;
  63.         }
  64.     }
  65.     for (int j = 0; j < 6; j++) {
  66.         b[2][j] = a[4][j] / a[5][j];
  67.         if (a[5][j] == 0) {
  68.             b[2][j] = 0;
  69.         }
  70.     }
  71.     cout << "Матрица, полученная делением четных строк на нечетные: " << endl;
  72.     for (int i = 0; i < 3; i++)
  73.     {
  74.         cout << endl;
  75.         for (int j = 0; j < 6; j++) {
  76.             cout << b[i][j] << " ";
  77.         }
  78.     }
  79.  
  80.     cout << endl << endl;
  81.  
  82.     for (int i = 0; i < 3; i++) {
  83.         c[i][0] = b[i][0] / b[i][1];
  84.         if (b[i][1] == 0) {
  85.             c[i][0] = 0;
  86.         }
  87.     }
  88.  
  89.     for (int i = 0; i < 3; i++) {
  90.         c[i][1] = b[i][2] / b[i][3];
  91.         if (b[i][3] == 0) {
  92.             c[i][1] = 0;
  93.         }
  94.  
  95.     }
  96.     for (int i = 0; i < 3; i++) {
  97.         c[i][2] = b[i][4] / b[i][5];
  98.         if (b[i][5] == 0) {
  99.             c[i][2] = 0;
  100.         }
  101.     }
  102.     cout << "Матрица, полученная делением четных столбцов на нечетные: " << endl;
  103.     for (int i = 0; i < 3; i++) {
  104.         cout << endl;
  105.         for (int j = 0; j < 3; j++) {
  106.             cout << c[i][j] << " ";
  107.         }
  108.     }
  109.     cout << endl << endl;
  110.  
  111.     for (int m = 0; m < (size * size - 1); m++)   //сдвиги очередных элементов в правильную позицию
  112.     /*сдвиг элемента массива в правильную позицию*/
  113.         for (int i = 0; i < size; i++) {
  114.             for (int j = 0; j < size; j++) {
  115.                 /*АНАЛИЗ НА ПОСЛЕДНИЙ ЭЛЕМЕНТ МАССИВА*/ 
  116.                 if (i == size - 1 && j == size - 1) {  //Если строка последняя и справа тупик, то ничего не делаем
  117.                     continue;
  118.                 }
  119.                 /*КОНЕЦ АНАЛИЗА НА ПОСЛЕДНЮЮ СТРОКУ*/
  120.  
  121.                 if (c[i][j] > c[i][j + 1]) { //Если элемент не на своей позиции
  122.                     temp = c[i][j];        //Обмен местами
  123.                     c[i][j] = c[i][j + 1];
  124.                     c[i][j + 1] = temp;
  125.                 }
  126.             }
  127.         }
  128.     swap(c[0][1], c[1][0]); //три пары элементов массива
  129.     swap(c[0][2], c[2][0]);
  130.     swap(c[1][2], c[2][1]);
  131.     cout << "Конечная матрица:\n\n";
  132.     for (int i = 0; i < 3; i++) {
  133.         cout << endl;
  134.         for (int j = 0; j < 3; j++) {
  135.             cout << c[i][j] << " ";
  136.         }
  137.     }
  138.     cout << endl;
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement