-5

So the problem states like this,we have an array and we should do 2 types of operations:

1) Adding value v,on segment [x,y]

2) Finding sum on segment [x,y]

I was searching for the answer but couldn't find anything,if you have any useful links or advice I'll be grateful.

OmG
  • 18,337
  • 10
  • 57
  • 90
filo
  • 1

1 Answers1

0

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.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335