[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using mod in a loop, C/C++

Posted on 2011-02-16
5
Medium Priority
?
835 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 2000 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:Deepu Abraham
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

649 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