I'm new to C++ language. I would like to random generate 5 numbers (between 0 and 50) and then calculate their sum. Could I see an example of this?
I know about the showSum(1,2,3) function. But if I generate 5 numbers and put them in a container or array, how could I showSum that? An example I would very much like to see. Where to start?
It is not a wee bit more generic than the range-based loop version.
Both std::accumulate( std::begin(array), std::end(array), 0 ) ;
and int sum = 0 ; for( int value : array ) sum += value ; accumulate values into an integer.
The only pedantic difference is that std::acumulate uses the + operator, the range-based loop uses the += operator.
Both std::accumulate( std::begin(vec), std::end(vec), decltype( +vec.front() ){} ) ;
And decltype( +vec.front() ) sum {} ; for( const auto& value : vec ) sum += value ;
would be more generic.