Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- Arrays
- File Input/Output
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
Updates
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
Lastly
Why Count Starting With Zero?
-
In GridToolkit, rows and columns were numbered starting with zero instead of one. The index that is used to identify a particular cell also started with the first cell numbered zero. This convention has been in place forever in the world of computer programming.
The reason is that this simplifies the code. Can you find the code in our GridWorld which is simpler than it would have to be if we started numbering the index at one instead of zero? How would the code need to be changed if the base was one instead of zero?
Answer:
The Bottom Line - by having cell numbers, column numbers, and row numbers start at zero, computing the location of cells is simpler.
; move the turtle to top-left corner of specified cell to gridGotoCell :idx gridGotoTopLeft setheading 180 forward product gridCellSize (int quotient :idx gridNumCol) setheading 90 forward product gridCellSize (remainder :idx gridNumCol) end
Given the grid above and the gridTotoCell procedure which moves the turtle to a specified cell, let's look at the code and work through a couple examples. First let's look at the edge cases.
-
First example: idx = 0, gridNumCol = 5,
Both forward instructions will result in no movement which is exactly what we want. Zero divided by anything results in zero for a quotient and also results in a zero remainder. So, the product of gridCellSize and zero always produces zero. -
Second example: idx = 14, gridNumCol = 5,
The first forward, moving south, starts with quotient 14 5 outputing 2.8. int with 2.8 as its input, outputs 2. This is exactly what we want, the turtle moves south two cells.
The second forward, now moving east, has remainder 14 5 outputing 4. This works too, moving the turtle to the east four cells.
So, how would this code need to change if we started our indexing at one instead of zero? Answer: we would have to subtract one from the index before using it. Here is what the forward instructions would look like.
forward product gridCellSize (int quotient (difference :idx 1) gridNumCol) forward product gridCellSize (remainder (difference :idx 1) gridNumCol)
Once again, the bottom line is that the code is simpler when cell numbers, column numbers, and row numbers start out at zero instead of one.
-
First example: idx = 0, gridNumCol = 5,