I have an array and I want to scroll the positions in it backward.
I have a boolean array and I need to count how many true are there from a given cell going forward and backward.
I know that if N is the number of elements into the array, I can go ahead doing i=(i+1)%N. But what if I need to scroll it in the opposite way?
Thank you in advance.
This should do it:
i--;
if (i == -1)
i = N-1;
Or in 1 line:
i = ((i-1 == -1) ? N-1 : i-1);
But noise's variation of the above is probably better. A related if-statement version:
if (i == 0)
i = N-1;
else
i--;
A related 1-line version:
i = (i == 0 ? N-1 : i-1)