Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an array work like a circle

Tags:

c#

I'm not quite sure how to do this.

pseudocode:

array1 = {"a","b", "c", "d", "e","f", "g","h", "i","j"} //there might be more values.
take c
loop =>c+3 =f
       f+3 =i
       i+3 =b
       b+3 =e
......
end loop

I need to work this array1 as a circle and find the letters adding 3 (f, i, b, e, and more).

like image 634
Giliweed Avatar asked Nov 26 '25 22:11

Giliweed


1 Answers

Use mod (%), then index can be any positive value and it will wrap around:

int index;
array1[index % array1.Length]
like image 123
weston Avatar answered Nov 29 '25 11:11

weston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!