AMSTRAD cpc 6128 User Instruction page 108

Integrated computer/disc system
Hide thumbs Also See for cpc 6128:
Table of Contents

Advertisement

But hang on! We said that we want to be able to store up to 100 names and 100 'phone
numbers in the program. Surely we don't have to write a program with two hundred
I N PUT commands, each with a different variable name, and then two hundred
PR I N T commands to produce a list on the screen??? Don't worry, computers make
provision for this with what's known as an 'array'. An array allows you to use one
variable (such as N A M E $) which can have any number of 'dimensions' (in our
program, we require 100). Then, when you want to get at the contents ofthe variable,
you use the variable's name followed by its reference number (inside brackets). This
reference number is called a 'subscript' and the expression N A ME $ ( 27) for
example, is called a 'sub scripted variable'. Now if we use a number-variable as the
subscript, for example N A ME $ ( x ) , we can then process the whole list of variables
from 1 to 100 by changing the value of x in a FOR NE X T loop, i.e. FOR x
=
1 TO 1 00.
As the value of x increases by 1, so the subscript value changes and refers to a
different 'element' or name in the N A M E $ array.
We want two arrays; one for N AM E $, and one for T E L$, each with a dimension of 100
elements. Before we can start using an array, we must declare its DIMensions at the
outset. We'll overwrite lines 20 and 30 with these statements:
20 DIM NAME$(100)
30 DIM TEL$(100)
Having established our variables, let's write some program that will firstly enable us
to enter the names and numbers into the arrays, for later retrieval. Add:
40 FOR x=1 TO 100
50 INPUT;" name";NAME$(x)
60 INPUT" phone";TEL$(x)
70 NEXT
run
This is all very well, but we may not want to enter all 100 names at once. What's
more, the way that the program presents itself on the screen is most unsatisfactory.
What's needed here is a bit of tidying up. Firstly, before taking input of each new
name and number, let's rid the screen of all the previous superfluous text, by
C Learing'the Screen each time. Add:
45 CLS
Now, how do we get the computer to know that we've finished inputting information
for the moment? Pressing
[ESC]
would stop the program alright, but as soon as you
typed RUN again, you'd lose all the values of your carefully entered variables!
Beyond Foundations
Chapter 2 Page 3

Advertisement

Table of Contents
loading

Table of Contents