Programming C++
Instead of declaring and defining the variable r at the beginning of the function, declare it inside the loop that uses it. With this approach, r does not need to be initialized to 0 because you can assign a meaningful value directly.
(This saves only a small amount of code, but it does make sense.)