Solved

Using mod in a loop, C/C++

Posted on 2011-02-16
5
821 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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