Advertisement
Lavig

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

Mar 6th, 2025
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. double* product(double arr[], int len) {
  7.     double* result = new double[len];
  8.     result[0] = arr[0];
  9.     for (int i = 1; i < len; i++) {
  10.         result[i] = arr[i] * result[i - 1];
  11.     }
  12.     return result;
  13. }
  14. int main()
  15. {
  16.     SetConsoleOutputCP(1251);
  17.     SetConsoleCP(1251);
  18.     int N{};
  19.     double total_product{ 1 };
  20.     string words_array[10]{ "перший", "другий", "третій", "четвертий", "п'ятий", "шостий", "сьомий", "восьмий", "дев'ятий", "десятий" };
  21.     while (true) {
  22.         cout << "Введіть бажану кількість елементів у масиві (від 2 до 10): ";
  23.         cin >> N;
  24.         if (cin.fail() || N < 2 || N > 10) {
  25.             cin.clear();
  26.             cin.ignore(32767, '\n');
  27.             cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
  28.             continue;
  29.         }
  30.         else {
  31.             break;
  32.         }
  33.     }
  34.     double* array = new double[N];
  35.     for (int i = 0; i < N; i++) {
  36.         while (true) {
  37.             cout << "Введіть " << words_array[i] << " елемент масиву: ";
  38.             cin >> array[i];
  39.             if (cin.fail()) {
  40.                 cin.clear();
  41.                 cin.ignore(32767, '\n');
  42.                 cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
  43.                 continue;
  44.             }
  45.             else {
  46.                 break;
  47.             }
  48.         }
  49.     }
  50.     double* result = product(array, N);
  51.     cout << "Новий масив: ";
  52.     for (int i = 0; i < N; i++) {
  53.         cout << result[i] << " ";
  54.         total_product *= result[i];
  55.     }
  56.     cout << endl << "Добуток чисел в новому масиві: " << total_product;
  57.     delete[] array;
  58.     delete[] result;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement