Open alexey-malov opened 8 years ago
for (unsigned i = 1; i < numbers.size(); i += 2)
{
numbers[i % numbers.size()] += positiveNumSum;
}
из элементов, стоящих на нечетных позициях, вычесть сумму всех неотрицательных элементов
Тесты должны проверять эту ситуацию
for (unsigned i = 1; i < numbers.size(); i += 2)
{
numbers[i % numbers.size()] *= 2;
}
Бонус начисляется за вывод элементов массива в порядке возрастания их значений
bool sortGreater(double a, double b)
bool vectorsAreEquals(vector<double> vector0, vector<double> vector1)
bool vectorsAreEquals(vector<double> vector0, vector<double> vector1)
bool vectorsAreEquals(vector<double> vector0, vector<double> vector1)
{
if (vector0.size() != vector1.size())
{
return false;
}
for (unsigned i = 0; i < vector0.size(); ++i)
{
if (fabs(vector0[i] - vector1[i]) > 0.00001 * fabs(vector0[i]))
{
return false;
}
}
return true;
}
bool vectorsAreEquals(vector<double> vector0, vector<double> vector1)
{
if (vector0.size() != vector1.size())
{
return false;
}
for (unsigned i = 0; i < vector0.size(); ++i)
{
if (fabs(vector0[i] - vector1[i]) > 0.00001 * fabs(vector0[i]))
{
return false;
}
}
return true;
}
http://www.viva64.com/ru/w/V104/print/ http://www.viva64.com/ru/w/V108/print/
BOOST_AUTO_TEST_CASE(does_not_change_vector_containing_zero_numbers)
{
vector<double> numbers = { 0, 0, 0 };
vector<double> copy(numbers);
ProcessVector(numbers);
BOOST_CHECK(vectorsAreEquals(numbers, copy));
}
// Вычитание из каждого нечетного элемента вектора сумму положительных элементов
void SubEvensElementsWithPositives(std::vector<double> &numbers)
for (unsigned i = 0; i < numbers.size(); i += 2)
{
numbers[i] -= positiveNumSum;
}
http://www.viva64.com/ru/w/V104/print/ http://www.viva64.com/ru/w/V108/print/
void MulOddsEltsBy2(std::vector<double> &numbers)
{
for (unsigned i = 1; i < numbers.size(); i += 2)
{
numbers[i % numbers.size()] *= 2;
}
}
vector<double> numbers = { -1, 0, -3, 0 };
vector<double> copy(numbers);
ProcessVector(numbers);
BOOST_CHECK(VectorsAreEquals(numbers, copy));
numbers = { 0, 0, 0 };
copy = numbers;
ProcessVector(numbers);
BOOST_CHECK(VectorsAreEquals(numbers, copy));
numbers = { 1, 2.5, 3, 4.7, 5, 6.9, 7, 8.2, 9, 0 };
vector<double> numbersRequired = { -46.3, 5, -44.3, 9.4, -42.3, 13.8, -40.3, 16.4, -38.3, 0 };
ProcessVector(numbers);
BOOST_CHECK(VectorsAreEquals(numbers, numbersRequired));
numbers = { -1, -53.21, 1, 24.112, -213.521, 9842.124 };
numbersRequired = { -9868.236, -106.42, -9866.236, 48.224, -10080.757, 19684.248 };
ProcessVector(numbers);
BOOST_CHECK(VectorsAreEquals(numbers, numbersRequired));
double positiveNumSum = 0;
for (double number : numbers)
{
positiveNumSum += (number > 0) ? number : 0;
}
bool VectorsAreEquals(const vector<double> &vector0, const vector<double> &vector1)
{
if (vector0.size() != vector1.size())
{
return false;
}
for (unsigned i = 0; i < vector0.size(); ++i)
{
if (fabs(vector0[i] - vector1[i]) > 0.00001 * fabs(vector0[i]))
{
return false;
}
}
return true;
}