Search code examples
c++circular-reference

Circular array formula used in iterating months


Currently I found that using ( starting index + size of array + 1 ) % size of array will enable me to index the circular array from 0 to N ( size of array - 1 ).

Currently I'm using this to loop months from a specific month (i.e 6 = july). But the problem is this approach also print 0 which is not part of the proper month ( 1 - 12 ). I know I can do this with just if else statement but if possible I just want to modify the ( starting index + size of array + 1 ) % size of array formula to adapt to my needs if it is possible.


Solution

  • If as you say 6 = July then it seems pretty likely that 0 = January and 0 is in fact a perfectly valid value.

    If you mean to say 7 = July then I would actually suggest simply making your array index from 0-11 instead of 1-12. When changing between interfacing with the user and internal you would do the conversion between zero and one based indexing.