Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

asked on

Using mod in a loop, C/C++

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)
                    DoSomeWork(nItem);
            }

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?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

If you had in the loop
   if( nIdx % nCurrent ) dosomething();
and if nCurrent = 5, then dosomething would be called for nIdx = 1,2,3,4,6,7,8,9,11,12,13,14,16..
So, you would be skipping nIdx for all multiples of 5, since, for example, 15 mod 5 = 0 (remainder).

I didn't see from your question that this is what you wanted.

You could just  DoSomeWork(5) before the loop begins, and then exclude the 5 inside the loop with
    if( nIdx == nCurrent ) {
        continue; // skip to end of loop and start from beginning of loop
    }

If, for some reason, you had to have DoSomeWork exclusively inside the loop, then you could add a first_flag control variable initialized to true, and then add some extra logic; but I'd try to do the special case first outside of the loop.
Substituted in line 6 of my solution, you can use either of  these "mod" expressions depending on whether you want wraparound or sequential behavior.  

// Do nCurrent and keep going with wraparound:

(nCurrent+nIdx)%maxN				// 5 6 0 1 2 3 4

// Do nCurrent and then start at zero and do the rest:

(nIdx?(nIdx-(nIdx<=nCurrent)):nCurrent)		// 5 0 1 2 3 4 6

Open in new window

If you want to skip executing of DoSomeWork() for numbers that divide by 5 (5,10,15,etc.), use this:

 int nCurrent = 5; // could be any number retrieved from a global variable

          for (int nIdx = 0; nIdx < GetNumItems(); nIdx++)
          {
                int nItem = SomeArray[nIdx];

                if(nldx == 0 || nIdx % nCurrent != 0)
                    DoSomeWork(nItem);
            }
you might also have to check this so as to handle the boundary/limit

I took sjklein42 snippet to modify this.

int index = (nCurrent+nIdx)%maxN;
if (index <= maxN)
int nItem = SomeArray[index];

Open in new window