Arrays and Indexing
A common way to refer to many instances of the same thing is to give them a single name and index them.  So we have Super Bowl XX, Pope John 23, Taco Bell Franchise 229, etc.  Indexing is handy in programming

Indexing, The Basic Idea
Motivation:  When there is a large number of similar things that must be referenced and manipulated, it can be inconvenient to think up a unique name for each, and to refer to them by the name
For example:  Each of the Seven Dwarfs
has a name, but who can remember them?
Also, it is difficult to refer to them in a loop
since there is no way to enumerate them
Indexing names the items by associating a base name and a number -- the index -- with each one
Computer notation:  Dwarf(5) Û Happy

Indexing Particulars
In everyday indexing, it is common to begin the indexing with 1, e.g. May 1, SuperBowl I, Elizabeth I
The number at which indexing begins is its origin
Some computer languages use 1 as the origin, but others, including Visual Basic 6.0, use 0 as the index origin.  (Gripe: a decent language lets you decide, but whatever.)

Arrays
When a variable is indexed it is called an array
Arrays are used for representing collections of data values, e.g. integers, strings, etc.
For example: dwarf(0) = “Sneezy”
  dwarf(1) = “Dopey”
  dwarf(2) = “Grumpy”
...
Elements of an array must all be of the same type
The index of an array element is also known as a subscript

Arrays In VB6.0
Arrays are declared like any other variable using a Dim statement
  Dim dwarf(6) As String
Notice
The syntax is just like a normal declaration except for the parenthesis pair
In the parentheses is the largest desired index
The total number of elements of the array will be one more than the largest index, since the origin is 0
The type applies to all of the elements

Indexing Arrays
To refer to different elements of the array, it is necessary only to change the index …
The index value must be an integer constant (1), a variable (myNdex) or expression (myNdex+1)
A loop can sweep through all elements

Combining Indexing, Arrays, Loops
A common error is to is to index beyond the end of the array ...

Mini-Exercise #1
Declare a variable that holds 20 doubles (i.e. numbers with a decimal place) that represent weights.

Mini-Exercise #1 -- Answer
Declare a variable that holds 20 doubles (i.e. numbers with a decimal place) that represent weights

Mini-Exercise #2
Initialize the array weights with values of 100, 200, 300, … 2000

Mini-Exercise #2 -- Answer
Initialize the array weights with values of 100, 200, 300, … 2000

Mini-Exercise #3
Print out the contents of the array of weights

Mini-Exercise #3 -- Answer
Print out the contents of the array of weights

Practice Using Arrays
Draw a 10-segment “inch worm” on the screen and move it forward
Use arrays to keep the positions of the segments
Write procedures to initialize worm and draw it
Goals of exercise:
Practice with arrays
Practice with indexing
Practice writing procedures
Notice how arrays are passed
as parameters

Worm code
The code for this example is available on the class web under “Example Code”

Worm Programming – Global Variables
The first step is to declare two arrays.  These hold the x and y coordinates of the center of each of the circles that make up the body of the worm.

There are 10 circles in all.

Worm Programming – Initialization
Initialize the worm on Form load:

initializeWorm procedure

drawWorm procedure

Making the worm crawl