Chapter 6
Arrays
Arrays
The variables seen so far are elementary .
They contain only one value of a simple type.
It happens that we are obliged to process several
data of the same type belonging to the same
entity.
Arrays
Example :
Let's imagine that we need 10 values simultaneously (for
example 10 marks to calculate an average ).
The only solution we have is to declare 10 variables (N1, N2,
…N10, etc ).
Avg = (N1+N2+N3+N4+N5+N6+N7+N8+N9+N10 ) / 10
This is annoying , especially if it was with hundreds or
thousands of values to process.
Fortunately, programming allows us to put all of these
variables into one, called an array (vector).
Arrays
Definitions
10 -1 68 0 -32
1 2 3 4 5
A vector (one-dimensional array) is a
subscripted variable allowing you to
store n values of the same type .
The maximum number of elements, specified
in the declaration, is called the array
capacity .
The type of the array is the type of its
elements.
Arrays
Definitions
10 -1 68 0 -32
1 2 3 4 5
The position of an element is called index or
rank of the element.
An array has a set of indexs. Each value of the
index corresponds to only one box in the
array, therefore one element.
Arrays
Remarks
All elements of an array have the same name
(that of the array ).
All elements of an array have the same type
(integer array, character array )
The index must be a positive integer
Arrays
Declaring an array
Syntax
Variable_name : array [1 .. capacity] of Type
Variable_name : indicates the name of the array
array: indicates that it’s an array (and not a simple
variable)
capacity: indicates the capacity of the array.
Type: indicates the type of the array elements.
Arrays
Examples
Var
Vect : array [1..5] of integer
Vect is an array of 5 integers
T1 : array [1..10] of char
T1 is an array of 10 character
Tab: array [1..100] of real
Tab is an array of 100 boxes of real type
Arrays
Examples
Vect : array [1..5] of integer ;
10 -1 68 0 -32
1 2 3 4 5
Arrays
Remarks
Vect : array [1..5] of integer
An array may not be completely filled
12 -51 6
1 2 3 4 5
but it can never contain more elements than
the number provided during the declaration.
10 -1 68 0 -32 39 48
1 2 3 4 5
X X
Arrays
Access to elements (Index access)
To identify an element among the others, we
use an index: an integer which allows access
to an element.
To read an element or modify it, indicate the
value of its index.
Arrays
Examples
Let Vect : array [1..5] of real.
And let a be a real and b an integer.
a := vect [1] expresses that the variable a receives the
value of element n°1
10 -1 2.5 13 1.5 10
1 2 3 4 5 a b
Arrays
Examples
vect [4] :=34.56 expresses that element n° 4 of the
vect array receives the value 34.56
34.56
1 2 3 4 5
b:=3
a := vect [b] expresses that a receives the value of
element n° 3
10 -1 2.5 13 1.5 2.5 3
1 2 3 4 5 a b
Arrays
Remark
Be careful of overflow
In an array of capacity n, an index i must always be
between 1 and n (1 ≤ i ≤ n).
Arrays
Reading elements from an array
To fill the ith box of an array vect by a value
entered by the user, you must use the read
instruction : Read( vect [i])
To fill all the elements of an array, it is necessary to
use a loop. Since the capacity of the array is known,
then the “FOR” loop is the most appropriate for this
processing but nothing prohibits using the other
loops.
Arrays
Filling a vect array of 10 real.
Program read_array ;
Var
i:integer;
vect : array[1..10] of real;
Begin
for i :=1 to 10 do
begin
write ('Give element n° ',i);
read ( vect [i]);
end;
END.
Arrays
Give element n°1
Memory 2 2 Screen
Give element n°2
0 0
Give element n°3
-5 -5
Give element n°4
2.5 2.5
Give element n°5
-8
-8
Give element n°6
11
11
Give element n°7
20
20
Give element n°8
1
1
Give element n°9
-9
-9
Give element n°10
7
7
Arrays
Display elements of an array
To display the ith box of a vect array , you must use
the write instruction : Write ( vect [i])
To display all the elements of an array, it is necessary
to use a loop. Since the dimension of the array is
known, then the “FOR” loop is the most appropriate
for this processing but nothing prohibits using the
other loops.
Arrays
Displaying of vect array of 10 real
2 0 -5 2.5 -8 11 20 1 -9 7
Program display_array ;
Var
i:integer;
vect : array[1..10] of real;
begin
for i :=1 to 10 do
write ('element n° ', i, '=', vect [i]);
END.
Arrays
Screen element n° 1=2
element n° 2=0
element n° 3=-5
element n° 4=2.5
element n° 5=-8
element n° 6=11
element n° 7=20
element n° 8=1
element n° 9=-9
element n° 10=7
Arrays
Example
Write a program that allows you to:
Fill in a "temp" array with daily temperatures for a
week.
Calculate and display average temperatures.
Arrays
Program Temperature;
Var
temp : array [1..7] of real ;
i :integer ;
m,s : real;
Begin
S:=0;
for i :=1 to 7 do
begin
write('Give the temperature of day n°', i);
read( temp [i]);
s:=s+temp[i]
End;
m :=s/7;
write('The average of the week=', m);
END.
Program temperature; 2 0 -4 2.5 1 1.5 4 temp
Var 1 2 3 4 5 6 7 i
temp : array [1..7] of real;
0 2 2 -2 0.5 1.5 3 7 s
i:integer; m,s : real;
begin 1 m memory
for i :=1 to 7 do
begin Give the temp . of day n° 1 2
write('Give the temp. of day n°', i); Give the temp . of day n° 2 0
Give the temp . of day n°3 -4
read( temp [i]);
Give the temp . of day n°4 2.5
end;
Give the temp . of day n°5 1
s :=0 ; Give the temp . of day n° 6 1.5
for i :=1 to 7 do Give the temp . of day n°7 4
s :=s+temp [ i ] ;
m := s/7; Average of the week=1
Screen
write (' Average of the week=', m);
END.