The both assignments can be done by using standard algorithms and lambda expressions or that is in fact equivalent by using the range based for statement.
The approach with using algorithms and lambda expressions
#include <algorithm>
std::for_eqch( std::begin( a ), std:;end( a ),
[=]( int &z ) { if ( x <= z && z <= y ) z += v; } );
#include <numeric>
int sum = std::accumulate( std::begin( a ), std::edn( a ), 0,
[=]( int acc, int z ) { return acc + ( x <= z && z <= y ? z : 0 ); } );
The approach with using the range based for
for ( int &z : a )
{
if ( x <= z && z <= y ) z += v;
}
int sum = 0;
for ( int z : a )
{
if ( x <= z && z <= y ) sum += z;
}
In fact you can use any form of loop to do the assignments.:)