Solved

Using mod in a loop, C/C++

Posted on 2011-02-16
5
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

717 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