math - One arithmetic operation needed to refer to neighbours in Array -
i got dynamically sized array [2,5,3,4,6,7]
now want reffer left , right neighbour of element. example got index = 3
which element value "4". right neighbour can make
index + 1 mod length = 4
if @ right end, index = 5, 5 + 1 mod 6 = 0. takes first element in array. want. want achieve same left neighbour. how can this?
if got index = 0, have index - 1 = -1. how indexnumber of element on right side 1 arithmetic operation? can calculate operation every time on each index.
thank much!
(length + index - 1) mod length should trick.
so, when length = 6:
- if index = 0 left_index = (6 + 0 - 1) mod 6 = 5
- if index = 4 left_index = (6 + 4 - 1) mod 6 = 3 ...
Comments
Post a Comment