liveBook · Manning

Remainder operation

As explained in Grokking Data Structures

The modulo operator computes the remainder after integer division.

The remainder operation finds what is left after dividing one positive integer by another. Written with %, it helps a circular queue wrap an imagined index back into a static array. section 9.7

Use the divisor as the array length:

  • For an eight-element array, indexes run from 0 through 7. section 9.7
  • Convert a virtual index to a real index with virtual_index % 8. section 9.7
  • This lets a rear or front pointer re-enter at index 0 after passing index 7. section 9.7

Wrapping index 8

Because 8 % 8 == 0, virtual index 8 refers to array index 0. Likewise, 9 % 8 == 1 and 10 % 8 == 2. section 9.7

How Grokking Data Structures covers it

How deep it goes
Defines remainders for positive integers and works through wraparound examples, without developing modular arithmetic beyond array indexing.
Examples
Uses an eight-element array with 8 % 8 == 0, 9 % 8 == 1, and 10 % 8 == 2.
What it assumes
Assumes familiarity with integer division, array indexes, and queue pointers while clarifying the remainder operation itself.
What you can do after
Apply modulo to convert virtual queue positions into valid static-array indexes.
How it connects
Connects modulo arithmetic with circular queues, static arrays, array indexes, and queue pointers.

Also explained in

Compare how 3 books explain remainder operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.