Using mod in a loop, C/C++
Posted on 2011-02-16
Let’s say I have some code like below:
int nCurrent = 5; // could be any number retrieved from a global variable
for (int nIdx = 0; nIdx < GetNumItems(); nIdx++)
int nItem = SomeArray[nIdx];
if(nIdx != nCurrent)
The code above is not complete but basically I want to do the global first in DoSomeWork then all the other items from the array in sequence. In other words the first time through the loop the function DoSomeWork should be DoSomeWork(5), then iterate through the loop dependent on the number of items from GetNumItems. If GetNumItems where to be say 6, then the first time through 5 would go into DoSomeWork followed, in any order, 0,1,2,3,4 and 6 not repeating 5.
Someone suggested to start the loop at the nCurrent number then use the mod operator before referencing the array. Is there any way to use the mod for the above example?