This article is part of the Getting Started series |
|
An array (sometimes vector or unidimensional table) is a row of more elements of the same type. Each element has an indice, which can be either an integer number or a character. A matrix (or bidimensional tables) is an array of arrays. This can be useful when dealing with number tables or even string tables. A block o variables (or tridimensional table is a array of arrays of arrays. This is rarely used but may sometimes be useful when defining shapes of 3D-objects.
Pascal allows creation of 4-dimensional, 5-dimensional, 6-dimensional, etc. arrays but they are extremely rarely used.
Defining and using[]
The uni-, bi-, and tri-dimensional tables are defined as it folows:
v: array [min_indice..max_indice] of element_type; {vector}
m: array [min_indice..max_indice,min_indice2..max_indice2] of element_type; {matrix}
b: array [min_indice..max_indice,min_indice2..max_indice2,min_indice3..max_indice3]
of element_type;{block}
To access the contents of an element in the table, it will be accesed as it follows:
x:= name_of_the_table[position1,position2,position3,...,positionN];
For example, if we consider an integer vector and want to show the element on the 3rd position, then the script will be as it follows:
var v: array[1..50] of integer;
{...}
begin
{...}
write (v[3]);
{...}
end.
Example script: reading and showing on the screen the contents of a vector[]
var v:array[1..50] of integer;
i,n:integer;
begin
writeln ('How many elements will be used?');
readln (n);
{reading, using a FOR...DO cycle}
for i:=1 to n do
begin
write ('v[',i,']=');
readln (v[i]);
end;
{showing on the screen, using another FOR..DO}
for i:=1 to n do
write (v[i],' ');
readln;
end.