Re:Edsger Dijkstra's note on starting array indices at 0 (pd...
by graywh on Tuesday June 21, 2011 @04:13PM
(#2680202)
It would have to be ((i - 1) % N) + 1
Re:Edsger Dijkstra's note on starting array indices at 0 (pd...
by scott_s on Tuesday June 21, 2011 @04:22PM
(#2680233)
Why? If all I want is a mapping from i to my index space, I don't see why we should have to shift i down one first. Note that we don't have to come up with the same mapping that the zero-based indexing would.
Re:Edsger Dijkstra's note on starting array indices at 0 (pd...
by axiak on Tuesday June 21, 2011 @07:05PM
(#2680781)
Because then you'd get to the next row before you've finished N elements.
Re:Edsger Dijkstra's note on starting array indices at 0 (pd...
by scott_s on Tuesday June 21, 2011 @07:34PM
(#2680858)
Yeah, I don't see what you're getting at. Where does "before" come in? We're assuming an arbitrary positive integer, not a sequence. And even if it was a sequence, I see no problems. Let's assume N == 5, then the mappings we get are:
1 -> 2
2 -> 3
3 -> 4
4 -> 5
5 -> 1
6 -> 2
7 -> 3
8 -> 4
9 -> 5
10 -> 1
...
This is a valid mapping from Z+ to our index space. Is your objection that 2 does not map to 2, 3 does not map to 3? That may be a useful property, but (i % N) + 1 is still a valid mapping without that property.
Of course, if it was indexed from 1 to N, then (i % N) + 1 would do the trick, but that's more cumbersome and uglier for what can be a frequent operation.