Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <format>
- #include <vector>
- #include <iomanip>
- using namespace std;
- double calculations(const vector<vector<double>>& matrix) {
- double sum = 0;
- int lines = matrix.size();
- int columns = matrix[0].size();
- for (int i = 0; i < lines; i++) {
- for (int j = 0; j < columns; j++) {
- sum += matrix[i][j];
- }
- }
- return sum;
- }
- long double calculations(const vector<vector<double>>& matrix, int) {
- long double product = 1;
- int lines = matrix.size();
- int columns = matrix[0].size();
- for (int i = 0; i < lines; i++) {
- for (int j = 0; j < columns; j++) {
- product *= matrix[i][j];
- }
- }
- return product;
- }
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- srand(time(0));
- int i{}, j{}, lines{}, columns{};
- while (true) {
- cout << "Введіть кількість рядків масиву (від 2 до 10): ";
- cin >> lines;
- if (cin.fail() || cin.peek() != '\n' || lines < 2 || lines > 10) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Кількість рядків було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- while (true) {
- cout << "Введіть кількість стовпців масиву (від 2 до 10): ";
- cin >> columns;
- if (cin.fail() || cin.peek() != '\n' || columns < 2 || columns > 10) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Кількість стовпців було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- vector < vector <double> > matrix(lines, vector <double>(columns));
- cout << "Згенерована матриця: " << endl;
- for (i = 0; i < lines; i++) {
- for (j = 0; j < columns; j++) {
- matrix[i][j] = (rand() % 19 - 9) + (rand() % 99 + 1) / 100.0;
- cout << format("{:12.2f}", matrix[i][j]) << " ";
- }
- cout << endl;
- }
- cout << endl;
- cout << "Сума елементів матриці: " << calculations(matrix) << endl;
- cout << "Добуток елементів матриці: " << calculations(matrix, 0) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement