Gr11IT Delphi Chapter7 Arrays
Gr11IT Delphi Chapter7 Arrays
Chapter 7
Arrays
Introduction
All programming languages have structures
available to store data in numbered lists. A data structure refers to the way
data is organized. An example of
These numbered lists keep data in memory
a data structure we have already
while a program is running. The program can
manipulate and refer to the data again and
again when required.
In Scratch the data structure used to store a list of values is called a List.
You will recall from work we did in Chapter 4 that the string values in the Items
!"#
$ !!% !& '() *+%- $ !!% !& '/()
*+0 -$ !!% !& '1() *+2-
# * 2 0 ) 3
same way.
& 5 2 '() +//689;-
5 2 '/() +//<8=>-
arrNames
James Dean Samantha Kagiso
'/( '1( 'L( 'P( 'L(
? # *FH
? An array can contain a number of values while a single variable can only contain
a single value.
? All the elements of an array must be of the same data type, for example all
strings or all integers.
? # "
type such as Integer or Char. The numbers referring to the elements of an array
) HJ )
and String cannot be used as indices.
? K ) )
the index of the element in the array
& *H 'L( Ld element in the array,
) ) *+% - )
? K J )
only one index value.
QU6HV8'2&X&(U< #VZ
H 8
? ArrName is the name of the array.
? LowerIndex
UpperIndex
? LowerIndex and UpperIndex must be of an ordinal data type and are separated by
"
? UpperIndex must always be larger than LowerIndex.
? <BaseType> refers to the data type of the array elements and can be any available
data type in Delphi.
2 ) * )
be stored in these arrays.
arrWeight arrSymbols
1 88.5 A 10
2 120.8 B 5
3 65.6 C 12
… 55.0 D 7
9 73.8 E 5
10 66.9 F 6
arrAnswer arrTemp
10 C -2 12
11 D -1 8
12 A 0 4
… B …
19 B 79 45
20 C 80 22
The actual values you want the array to store are assigned to the array elements by
) & )*
B ) *
const
arrMonthName : array[1..12] of string = ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October',
'November', 'December');
var
iCount : integer;
sAllTheMonths : string;
begin
sAllTheMonths := ''; //Initialise the string to an empty string
Randomize;
//Add each month and an <enter> character to the string
for iCount := 1 to 12 do
sAllTheMonths := sAllTheMonths + #13 + (arrMonthName[icount]);
ShowMessage(sAllTheMonths);
end;
We can adjust our prizes example program by using a constant array. In this case the
array can be declared as follows:
implementation The array has unit scope to allow the code of
{$R *.dfm} more than one event handler to refer to the
const values in the array.
arrPrizes : array[1..5] of string
=('Justin Bieber concert tickets', 'iPad', '8 GB Flash disk', 'Chocolate gift pack',
'R5000 clothing voucher');
unit frmClassMarks_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
const
MAXNUM = 10;
type & )
TfrmMarks = class(TForm) class, the constant value has to be declared
private in the interface of the unit above the class
arrNames : [1..MAXNUM] of string; # ) )
public +*- )
end; value will be used.
The number of elements that is read into the array can then be controlled through
program code using the constant value.
repeat
iNumNames := StrToInt(InputBox('Enter names','How many names ? Maximum is ' +
IntToStr(MAXNUM),IntToStr(MAXNUM)));
until iNumNames <= MAXNUM ;
Open the program frmClassMarks_p provided with the data for this chapter. The
number of names to be entered has not been validated in this program. Execute
the program and enter a number that is bigger than the number of elements the
array can keep.
" \ )
than the maximum number of values declared in the array?
" 6 )
)" K
" K
" 6) ) )
* )) )
const
MAXNAMES = 25;
Decide on the maximum number of elements the
) ) ) )
type
frmChooseWinner = class(Tform)
...
private
arrNames : array[1.. MAXNAMES] of string;
iCountNames : integer;
end;
Declare the array and a counter with class scope.
1 $ ) } )
) ) 6 )
) ) ) ) \ )
code to do the following:
? [ ) * )
products.
? *
? Display the values in an appropriate output component.
? 3 ) ) ) *
increase.
? Increase all the values in the array with the percentage that has been
entered.
? Display the increased values in an appropriate output component.
L 2 ) ) )
<) ) ) )
program to display the names of the learners in a random sequence.
Design an interface and write code to do the following:
? /
? Display the names in a random sequence in an appropriate output
) # ) } )
will appear twice.
9 2 * ) ) ) # /
@ ) 6 < [\ K
)) }) @ " *
6 "
# -
by the learner and test the answers against the memorandum. Hint8X
X[ ) * ) }
[) - * *
appropriate output component.
6' (<} )) - )
be entered.
Challenge80 J
Examples:
? The user must be able to enter the memorandum only once.
? It must be possible to enter the name of the learner only once the
memorandum has been entered.
? # - ) -
been entered.
var var
arrVotes : array[0..5] of integer; arrVotes : array[1..6] of integer;
The following code will increase the value of the perfume voted for:
arrPhoneticAlph
A Alpha
& ) ) ) "
B Bravo
C Charlie
...
Y Yankee
Z Zulu
)" 2 ) ) " 3 49
4 16
" K )
5 11
private
arr Password : array[1..10] of char; Array with class scope.
var
sInString : string;
iLast, iCount : integer;
Code in event handler.
begin
sInString := edtRead.Text;
iLast := length(sInString);
for iCount := 1 to iLast do
arrPassword [iCount] := sInString[iLast];
end;
Element One of the variable values in an array which can be accessed by giving the
name of the array followed by the index of the element.
Flag 6* ) ) &
) ) )
not.
One-dimensional array 6 ) ) * " )
element can be accessed individually using a single index value.