Solved

Using mod in a loop, C/C++

Posted on 2011-02-16
5
808 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:atomicgs12
5 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34913586
int nCurrent = 5; // could be any number retrieved from a global variable

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

	DoSomeWork(nItem);
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 34913609
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.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34913777
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

0
 

Expert Comment

by:Zord_X
ID: 34913948
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);
            }
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34914684
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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now