Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- SetConsoleOutputCP(1251);
- srand(time(0));
- int N{}, i{}, min_negative{ 0 }, positive_sum{}, positive_count{};
- while (true) {
- cout << "Введіть бажану кількість елементів у масиві (від 2 до 10): ";
- cin >> N;
- if (cin.fail() || cin.peek() != '\n' || N < 2 || N > 10) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- int* array = new int[N];
- cout << "Згенерований масив: ";
- for (i = 0; i < N; i++) {
- *(array + i) = rand() % 100 - 50;
- cout << *(array + i) << " ";
- if (*(array + i) < min_negative) {
- min_negative = *(array + i);
- }
- if (*(array + i) > 0) {
- positive_sum += *(array + i);
- positive_count++;
- }
- }
- cout << endl;
- if (min_negative == 0) {
- cout << "У масиві немає негативних елементів" << endl;
- }
- else {
- cout << "Найменший негативний елемент масиву: " << min_negative << endl;
- }
- if (positive_count == 0) {
- cout << "У масиві немає позитивних елементів" << endl;
- }
- else {
- cout << "Середнє арифметичне позитивних елементів масиву: " << static_cast<double>(positive_sum) / positive_count << endl;
- cout << "Позитивні елементи масиву: ";
- for (i = 0; i < N; i++)
- if (*(array + i) > 0){
- cout << *(array + i) << " ";
- }
- }
- delete[] array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement