|
|
|
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 |
|
|
|
|
|
|
|
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 |
|
|
|
|
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.) |
|
|
|
|
|
|
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 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 |
|
|
|
|
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 |
|
|
|
|
A common error is to is to index beyond the end
of the array ... |
|
|
|
|
Declare a variable that holds 20 doubles (i.e.
numbers with a decimal place) that represent weights. |
|
|
|
|
|
|
|
|
Declare a variable that holds 20 doubles (i.e.
numbers with a decimal place) that represent weights |
|
|
|
|
|
|
|
|
Initialize the array weights with values of 100,
200, 300, … 2000 |
|
|
|
|
|
|
|
|
Initialize the array weights with values of 100,
200, 300, … 2000 |
|
|
|
|
|
|
|
|
Print out the contents of the array of weights |
|
|
|
|
|
|
|
|
Print out the contents of the array of weights |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
The code for this example is available on the
class web under “Example Code” |
|
|
|
|
|
|
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. |
|
|
|
|
Initialize the worm on Form load: |
|
|
|
|