Another way of achieving this sum?

Hi everybody I'm kind of new to C++ and I dont quite get this.

So I have a code wich sums the values from an array and the it outputs the result


1
2
3
4
5
6
7
8
    while(i < 10)
    {
        sum += valor[i];

        i++;
    }

    cout << sum << endl;


So is this the only way to achieve I mean only by using += operator?
Last edited on
Another way is to use the C++ function accumulate():

std::cout << accumulate(valor, valor+10, 0) << '\n';
demo: http://ideone.com/KFXZl
Just in case you don't realise, sum += valor[i]; is the same as sum = sum + valor[i];.
You can make a class,....wait,...it might just more complicated...
Topic archived. No new replies allowed.