Chapter
3:
Programming the
VIC
20
Computer 77
Arrays are used in many types of computer programs.
If
you are not
already familiar with arrays, you will need
to
learn
about
them. The infor-
mation
that
follows will be very important to your programming efforts.
Conceptually, arrays are very simple. When you have two or more
related data items, instead
of
giving each data item a separate variable name,
you give the collection
of
related
data
items a single variable name. Then you
select individual items using a position number, which in computer jargon
is
referred
to
as a subscript,
or
index.
A grocery list, for example, may have six items from the meat and
poultry department, four fruit
and
vegetable items, and three dairy prod-
ucts. These three groups of items could each be represented by a single
variable name as follows:
MPS(O)
=
"CHOPPED
SIRLOIN"
MPS(I)
= "CHUCK STEAK"
MPS(2)
= "NEW YORK STEAK"
MPS(3)
= "CHICKEN"
MPS(4)
= "SALAMI"
MPS(S)
= "SAUSAGES"
FVS(O)
= "ORANGES"
FVS(I)
=
"APPLES"
FVS(2) = "BEANS"
FVS(3)
= "CARROTS"
DPS(O)
=
"MILK"
DPS(I) =
"CREAM"
DPS(2) = "COTTAGE CHEESE"
MPS
is a single variable name that identifies all meat and poultry products.
FVS
identifies fruits and vegetables. while DPS identifies dairy products.
A subscript follows each variable name. Thus a specific data item is
identified by a variable name and
an
index.
Notice
that
the first index value in the examples above
is
0, not
1.
Subscripts
in
BASIC start from 0 because this simplifies the programming
of
many scientific and mathematical problems. Many people are un-
comfortable with this practice, however.
If
you don't feel
at
home with using
element 0 of
an
array, simply ignore it
and
start with a subscript of
1.
You
will waste a little memory space,
but
you are less likely
to
make program-
ming mistakes if you are not trying
to
"adapt"
yourself
to
the machine.
We could take the array concept one step further, specifying a single
variable name for the entire grocery list, using two indexes. The first index
(or sUbscript) specifies the product type, and the second index specifies the
item within the product type. This
is
one way in which a single grocery list
variable array with two subscripts could replace the three arrays with single
subscripts illustrated above.