You are on page 1of 2

Student notes 7.3.

Handling data in algorithms: Arrays


When a program requires a number of variables to store similar data with a similar name
and the same data type it is easier to declare an array of variables. In previous examples
when we require two numbers, they have been called num1 and num2. This is fine, but if
we required 20 such values it would be much simpler to declare an array with 20 values as
num(i).
If we consider an array with five elements, value(i), then for this data:
i 1 2 3 4 5
value(i) 123 329 106 59 87
value(3) is 106 and value(5) is 87.
We can use an index value to refer to any of these values, so we can deal with the whole
array within a suitable loop structure.
For example, if we take this array we can add them together using the following algorithm:
total=0
FOR i = 1 TO 5
total=total+value(i)
NEXT
An array will often be used to store data for use within a program, for example to search
for a particular value in a set of data.
Example:
If an array, name(), contains 10 items. An algorithm to search this data for a matching item
and report the location of the item in the list is:
Found=FALSE In VB.net, the command to declare
Count=1 an array for 20 integers is
INPUT Value to locate, Search Dim num(20) As Integer
WHILE Found=FALSE AND count<=10
IF name(count)=Search THEN In BBC BASIC the command is
OUTPUT value found at position; count DIM num%(20)
Found=TRUE
ENDIF (The % sign is used in BBC BASIC
count=count+1 to indicate that the value is an
ENDWHILE integer.)
IF Found=False THEN OUTPUT Value not found

Page 1 of 2
OCR Computing for GCSE Hodder Education 2011
Continued

To create the array and enter the data for the algorithm above the pseudocode is:
DIM array num(10)
For i = 1 TO 10
INPUT num(i)
NEXT i
The data in this array will be lost when the program is completed and it does not provide a
method for storing data. To store data, a file must be created on suitable storage media.
In order to use a data file the program must identify the name of the file, including its
location, whether the file is to be read from or written to, and a communication channel.
The commands to do this vary between programming languages.
There will be commands to:
open for write access (this will often allow a new file to be created if the file does not
exist);
open for read access;
close;
identify the end of the file.
For example, the user wishes to set the size of an array, enter the data and save it to a file.
INPUT size of array size In VB.net, there are various ways to
DIM name(size) create files, write and read data from files
FOR i =1 to size including the Createfile, Writefile and
INPUT name(i) Readfile APIs.
NEXT i
OPEN for write access file names.dat In BBC BASIC the commands follow a
FOR i= 1 TO size pattern: OPENIN, OPENOUT and CLOSE,
WRITE to file name(i) for example:
NEXT i chan1 =OPENIN c:\data.dat
CLOSE file names.dat INPUT#chan1,data
CLOSE#chan1
This opens a channel for input from the file
data.dat on the C drive, reads an item of
data in the variable data, then closes the
file.

Page 2 of 2
OCR Computing for GCSE Hodder Education 2011

You might also like