Every Other One

You should be familiar with the C language modulus operator, %. Even if that familiarity is fear or confusion it’s still familiarity.

Modulus is simply a diabolical-sounding term for “find the leftovers.” You first stumbled upon the modulus concept when learning long division: The modulus is related to the remainder, the leftover when one number doesn’t gazinta another equally. That concept is expressed in C programming by using the % (percent sign), the modulus operator.

The modulus operator is commonly used between two integer values or variables. The result is the leftover of the first integer divided by the second. But why bother resolving that issue in your head when you can stare at some code?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int r,x;

    srand((unsigned)time(NULL));    /* seed the randomizer */
    for(x=0;<<20;x++)
    {
        r = rand() % 10 + 1;        /* from 1 through 10 */
        printf("%2d\n",r);
    }

    return(0);
}

Line 12 fetches a random value by using the rand() function. That value can be quite huge, depending on the C library's implementation of rand(). To pare it down to the range of 1 through 10 the modulus operator is used:

rand() % 10

This equation takes whatever integer value rand() generates and divides it by 10. The remainder is in the range of 0 through 9. Line 12 in the code adds one to the result, which makes the range 1 through 10. The program's output proves that:

 5
 1
 4
 2
10
 2
 6
 7
 5
 4
 6
 9
 1
 2
 3
 1
 2
 5
 9
 3

It would be enough to leave the modulus operator alone at this point. The % makes a great slicer for the rand() function's return value, but that's not the limit of its abilities.

For example, consider this output, which is created using the same code shown above but with a few modulus-related modifications:

  6
* 3
  3
*10
  1
* 2
 10
* 2
  5
* 1
  4
* 9
  3
* 7
  7
* 2
  3
* 6
  7
* 8

See how every other line has an asterisk prefixed? Without peeking ahead, can you code such a thing? It's possible without using the modulus operator, but it's far easier when you use it.

Remember that the modulus operator gives you the remainder. So if you want to pluck out every nth item in a list, you use the formula % n. Here is the updated for loop from the preceding code example, the code that generates the every-other-line-asterisk output:

    for(x=0;x<20;x++)
    {
        r = rand() % 10 + 1;        /* from 1 through 10 */
        if(x % 2)
            printf("*%2d\n",r);
        else
            printf(" %2d\n",r);
    }

The if comparison is TRUE when the value of x is odd (not evenly divisible by two). So for every odd "row" in the output, an asterisk prefixes the value. The even output (the else condition above) puts a space in the printf() function, which keeps each value lined up.

Click here to see the full code.

Now what if you wanted to place an asterisk by every third item? Can you modify the code to do that? It might not be as easy as you think, but you'll figure it out.

Leave a Reply