Advertisement
Lavig

Другий семестр. Лабораторна робота №7-8 (Завдання 3)

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