Arrays; Dim - AMSTRAD CPC464 User Manual

Cpc464 colour personal computer 64k
Hide thumbs Also See for CPC464:
Table of Contents

Advertisement

And you will see that the border alternates between colours 14 and 6. You will have to wait until the
next chapter to get more explanation of the way graphics and colours work. To round off this sub-
section, enter:
i n k 1 , 1 8 , 1 6
then...
s p e e d 1 , 5
Now find the aspirin, and reset the computer. Remember to SAVE the program on the cassette if you
want to impress your friends.

4.11 ARRAYS

Some programs require a lot of variables to store data. This is all very well but it is often dificult to
keep track of which variable is being used to store which piece of data. Fortunately, BASIC provides
for this situation in the form of data arrays.
What is an array ? An array is basically a group of variables all referenced by the same name.
The best means of explanation is to take an example. Consider a program that simulates a card
game, and involves the dealing of randomly selected cards. Obviously the same card cannot be dealt
twice and so a record must be kept of each individual card. A simple way of doing this would be to
assign a variable to each card and then set the variable according to the card' s location - say 1 to
signify that it has been dealt and 0 to signify that it is still in the deck.
Evidently, this will involve 52 separate variables (unless you' re playing with a fixed deck!) and you
have to remember which variable refers to which card - this is where the array comes in useful.
Firstly, we need to give the array a name, say PACK. Now, in order to access a particular variable or
element in the array we simply give it a number. So if the first thirteen elements are used to define
the suite of hearts, then the six is represented by P A C K ( 6 ) the ten by P A C K ( 1 0 ) and the king
by P A C K ( 1 3 ) .
Getting clearer ?
You cannot, unfortunately, simply go on and on referencing more elements ad infinitum without
giving the computer fair warning to reserve space in memory. The DIM command is used to achieve
this.
This command DIMensions the array i.e. sets its size and so for our example of a pack of cards fifty
two elements need to be reserved.
The required BASIC is :

DIM

PACK (52)
This tells the computer to reserve space in memory for fifty two variables (fifty three actually since
element zero can also be accessed).

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents