Advertisement
malixds_

inf12

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