В векторе Y(N) определить каких элементов больше – положительных или отрицательных. Язык C++.
Ответы
// Вектор заполнил случайными числами и вывел на экран для наглядности
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
int main()
{
int n;
std::cout << "Enter n: ";
std::cin >> n;
std::vector<int> y;
std::srand(std::time(0));
std::cout << "vector: ";
for (int i = 0; i < n; ++i) {
int randNum = std::rand() % 100 - 50;
y.push_back(randNum);
std::cout << randNum << " ";
}
int negativeCounter = 0;
int positiveCounter = 0;
for (int i = 0; i < n; ++i) {
if (y[i] < 0)
negativeCounter++;
else if (y[i] > 0)
positiveCounter++;
}
std::cout << "\n\nAnswer: ";
if (negativeCounter == positiveCounter)
{
std::cout << "the number of positive and negative elements is the same\n";
return 0;
}
if (positiveCounter > negativeCounter)
std::cout << "the number of positive elements is greater\n";
else
std::cout << "the number of negative elements is greater\n";
}
