Archived

This topic is now archived and is closed to further replies.

UeberBobo

%?

Recommended Posts

Example when you have a variable containing the time in seconds and want to output it to the user as minutes and seconds.

time_t totalTimeInSeconds = 143;
time_t timeInMinutes = totalTimeInSeconds / 60;
time_t timeInSeconds = totalTimeInSeconds % 60;


[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

Share this post


Link to post
Share on other sites
it''s the modulus <sp?> operator. the most common use is for creating a loop out of an endless stream of increasing numbers:


int i = 0;
while (1) {
std::cout << i%3 << "\n";
i++;
}


that code will print:
0,1,2,0,1,2,0,1,2,0,1,2.....

a more practical example is for use in generating random numbers. if you want a number between 0 -> sumNumber, you call:

rand() % someNumber;

-me

Share this post


Link to post
Share on other sites
Thats the modulus operator, and you are correct that it gives you the remainder of a division equation. I find it very, very useful! I used it in a very cool instance while working on my current project which is a drop block style game. I was trying to come up with an algorithm to blit the bitmaps for my gameboard, which is a grid of spaces that I represented with an array of 187 integers. In order to determine the x coordinate of where to begin my blitting operations for each square I simply took the number of spaces (187) and took the modulus to see how many spaces in I needed to start my blitting. here''s a little snippet of the code to see if you can make some sense out of it:

SetRect(&rectSource, 0,0,640,480);
SetRect(&rectDest, 0,0,640,480);
pGfxEngine->BlitOffScreenSurface(pGfxEngine->lpDDS_FPBG,
rectSource, rectDest);
int x, y;
for (int i = 0; i<187; i++)
{

x = 183+((i%11)*25);
y = 28+(424 -(((i/11)*25)+24));

....blitting here
}


Hope that wasnt too confusing :-p Maybe some one could come up with a simpler example.

Share this post


Link to post
Share on other sites
Yep, it basically just gives the remainder of a division.

I used it in a couple calculators I made way back in the day to convert decimal to hex and binary, although I''m sure that there''s probably a function in the c files that will do that.

Share this post


Link to post
Share on other sites
If you're finding A % B and B is a power of 2, it's much faster to say A & (B-1). EDIT: Faster in terms of execution time, I mean.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on October 24, 2003 11:26:04 PM]

Share this post


Link to post
Share on other sites
Another use. Let''s say you have an array storing thousands of numbers that you want to print in a loop. If you print them one after the other without line breaks, the page will zoom off to the right. If you add line breaks after each, the page will stretch downward. Instead it would be nice to print the numbers in a block, with a line break after printing every Nth element of the array. A modulus can help you do that.

for ( int i = 0; i < length; i++) {
printf("%d ", i);
if ( !(i + 1 % N) )
printf("\n");
}

Two things to note - the % in the printf is not a modulus. In that situation the % symbol is used as a formatting escape marker for printf.

In the condition "if ( !(i + 1 % N) )", 1 is added to i to prevent taking the modulus of 0, which is always 0. What that condition says is that if the modulus is 0, that is, if there is no remainder, then print the newline. The boolean negation is needed to change 0 in to true. If 1 isn''t added to i, then a newline will be printed after the first element.

Another use. Say you have a ticker of some kind - a clock that ''beats'' at a constant rate and you want to perform some routine every 10th tick or every 8th tick or every Nth tick. You can use a modulus to check if it''s the Nth tick and if so then call the "Nth tick" routine.

Share this post


Link to post
Share on other sites