Advertisement
Lavig

Лабораторна робота №12 (Завдання 2)

Nov 21st, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <format>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     SetConsoleOutputCP(1251);
  11.     srand(time(0));
  12.     vector<double> C = {};
  13.     int i{}, j{}, lines{}, columns{};
  14.     double B{};
  15.     while (true) {
  16.         cout << "Введіть будь-яке число (від 1 до 98): ";
  17.         cin >> B;
  18.         if (cin.fail() || B < 1 || B > 98) {
  19.             cin.clear();
  20.             cin.ignore(32767, '\n');
  21.             cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
  22.             continue;
  23.         }
  24.         else {
  25.             break;
  26.         }
  27.     }
  28.     while (true) {
  29.         cout << "Введіть кількість рядків масиву (від 2 до 10): ";
  30.         cin >> lines;
  31.         if (cin.fail() || cin.peek() != '\n' || lines < 2 || lines > 10) {
  32.             cin.clear();
  33.             cin.ignore(32767, '\n');
  34.             cout << "Кількість рядків було введено неправильно. Спробуйте ще раз!" << endl;
  35.             continue;
  36.         }
  37.         else {
  38.             break;
  39.         }
  40.     }
  41.     while (true) {
  42.         cout << "Введіть кількість стовпців масиву (від 2 до 10): ";
  43.         cin >> columns;
  44.         if (cin.fail() || cin.peek() != '\n' || columns < 2 || columns > 10) {
  45.             cin.clear();
  46.             cin.ignore(32767, '\n');
  47.             cout << "Кількість стовпців було введено неправильно. Спробуйте ще раз!" << endl;
  48.             continue;
  49.         }
  50.         else {
  51.             break;
  52.         }
  53.     }
  54.     vector < vector <double> > A(lines, vector <double>(columns));
  55.     cout << "Задана матриця: " << endl;
  56.     for (i = 0; i < lines; i++) {
  57.         for (j = 0; j < columns; j++) {
  58.             A[i][j] = rand() % 199 - 99;
  59.             cout << format("{:7}", A[i][j]);
  60.             if (abs(A[i][j]) > B) {
  61.                 C.push_back(A[i][j]);
  62.             }
  63.         }
  64.         cout << endl;
  65.     }
  66.     cout << "Вихідний масив: " << endl;
  67.     for (i = 0; i < C.size(); i++) {
  68.         cout << C[i];
  69.         if (i < C.size() - 1) {
  70.             cout << ", ";
  71.         }
  72.     }
  73.     cout << endl;
  74.     cout << "Кількість елементів, що перевищують по абсолютній величині задане число - " << C.size();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement