BFOIT - Introduction to Computer Programming

Two New Built-in Operators - Why?

In the GridToolkit, I used two built-in operators that you have not seen before.  What are they?  Why did I need to use them?

The new built-in operators used are int and remainder. They are used in the procedure gridGotoCell.

1. INT
``` forward product gridCellSize (int quotient :idx gridNumCol)
```

What does it do?  The expression "quotient :idx gridNumCol" is intended to produce the row number.  The row number multiplied by the size of a cell is the distance the turtle needs to move southward (from the top of the grid) to arrive at the desired row.  What int does is to remove the fractional part of its input, a number.  Here is an example, a walk-through, to show what's going on.

Let's say we want to go to the top-left corner of cell 12 in a grid that has 8 columns (gridNumCol = 8).  Cell 12 is in the second row, so I'm going to need the turtle to move south from its position at the top-left corner of the grid a distance equal to the size of one cell.  Well... quotient 12 8 produces 1.5 and striping off the fractional part gets us the value we need: 1.  If we do not remove the fractional part, the turtle will move too far... past the top of the second row, to the middle of the cell's height.

2. REMAINDER
``` forward product gridCellSize (remainder :idx gridNumCol)
```

What does it do?  The expression "remainder :idx gridNumCol" produces the column number.  The column number multiplied by the size of a cell is the distance the turtle needs to move eastward to get to the desired column.

Let's extend the int walk-through...  At the point in the procedure where remainder is used (in a forward command), the turtle is positioned at the top of the desired row.  First the turtle's heading it set to east.  Then we need to move forward to the left-edge of the desired column.  Since there are eight (8) columns in a row, when you divide the desired cell number twelve (12) by eight you get one (1) with a remainder of four (4).  Four times the size of a cell gets the turtle to the left-side of the twelfth cell.