An array is a structure concept for custom data types. It groups elements of the same data type. You will use arrays a lot if you are dealing with lots of data of the same data type.
Lists[edit | edit source]
Notion[edit | edit source]
In general, an array is a limited and arranged aggregation of objects, all of which having the same data type called base type or component type. An array has at least one discrete, bounded dimension, continuously enumerating all its objects. Each object can be uniquely identified by one or more scalar values, called indices, along those dimensions.
Declaration[edit | edit source]
In Pascal an array data type is declared using the reserved word
array in combination with the auxiliary reserved word
of, followed by the array’s base type:
program arrayDemo(output); type dimension = 1..5; integerList = array[dimension] of integer;
Behind the word
array follows a non-empty comma-separated list of dimensions surrounded by brackets.[fn 1]
All array’s dimensions have to be ordinal data types, yet the base type type can be of any kind.
If an array has just one dimension, like the one above, we may also call it a list.
Access[edit | edit source]
A variable of the data type
integerList as declared above, holds five independent
Accessing them follows a specific scheme:
var powerN: integerList; begin powerN := 5; powerN := 25; powerN := 125; powerN := 625; powerN := 3125; writeLn(powerN); end.
program will print
125, since it is the value of
powerN that has the index value
Arrays are like a series of “buckets” each holding one of the base data type’s values.
Every bucket can be identified by a value according to the dimension specifications.
When referring to one of the buckets, we have to name the group, that is the variable name (in this case
powerN), and a proper index surrounded by brackets.
Character array[edit | edit source]
Lists of characters frequently have and had special support with respect to I/O and manipulation.
This section is primarily about understanding, as in the next chapter we will get to know a more sophisticated data type called
Direct assignment[edit | edit source]
String literals can be assigned to
array[…] of char variables using an assignment statement, thus instead of writing:
program stringAssignmentDemo; type fileReferenceDimension = 1..4; fileReference = array[fileReferenceDimension] of char; var currentFileReference: fileReference; begin currentFileReference := 'A'; currentFileReference := 'Z'; currentFileReference := '0'; currentFileReference := '9';
You can simply write:
currentFileReference := 'AZ09'; end.
Note, that you do not need to specify any index anymore.
You are referring to the entire array variable on the LHS of the assignment symbol (
This only works for overwriting the values of the whole array.
There are extensions allowing you to overwrite merely parts of a
char array, but more on that in the next chapter.
Most implementations of string literal to
char array assignments will pad the given string literal with insignificant
char values if it is shorter than the variable’s capacity.
Padding a string means to fill the remaining positions with other characters in order meet a certain size.
The GPC uses space characters (
' '), whereas the FPC uses
char values whose
ordinal value is zero (
Reading and printing[edit | edit source]
Although not standardized,
writeLn usually support writing to and reading from
array[…] of char variables.
This works because
text files, like the standard files
output, are understood to be infinite sequences of
char values.[fn 2]
array[…] of char is also, although finite sequence of
char values, individual values can be copied pretty much directly to and from
text files, not requiring any kind of conversion.
Primitive comparison[edit | edit source]
Unlike other arrays,
array[…] of char variables can be compared using
if user <> host then begin write('Hello, ', user, '. '); writeLn('My name is ', host, '.'); end else begin writeLn('No. That is my name.'); end;
This kind of comparison only works as expected for identical data types.
It is a primitive character-by-character comparison.
If either array is longer or shorter, an
= comparison will always fail, because not all characters can be compared to each other.
<> comparison will always succeed for
array[…] of char values that differ in length.
Note, the EP standard also defines the
NE functions, beside many more.
The difference to
<> is that blank padding (i. e.
#0 in FPC or
' ' in GPC) has no significance in
In consequence, that means using these functions you can compare strings and
char arrays regardless of their respective maximum capacity and still get the naturally expected result.
<> comparisons on the other hand look at the memory’s internal representation.
Matrices[edit | edit source]
An array’s base type can be any data type,[fn 3] even another array. If we want to declare an “array of arrays” there is a short syntax for that:
program matrixDemo(output); const columnMinimum = -30; columnMaximum = 30; rowMaximum = 10; rowMinimum = -10; type columnIndex = columnMinimum..columnMaximum; rowIndex = rowMinimum..rowMaximum; plot = array[rowIndex, columnIndex] of char;
This has already been described above. The last line is identical to:
plot = array[rowIndex] of array[columnIndex] of char;
It can be expanded to two separate declarations allowing us to “re-use” the “inner” array data type:
row = array[columnIndex] of char; plot = array[rowIndex] of row;
Note that in the latter case
row as the base type which is an array by itself.
Yet in the short notation we specify
char as the base type, not a
row but its base type.
When an array was declared to contain another array, there is a short notation for accessing individual array items, too:
var curve: plot; x: columnIndex; y: rowIndex; v: integer; begin // initialize for y := rowMinimum to rowMaximum do begin for x := columnMinimum to columnMaximum do begin curve[y, x] := ' '; end; end; // graph something for x := columnMinimum to columnMaximum do begin v := abs(x) - rowMaximum; if (v >= rowMinimum) and (v <= rowMaximum) then begin curve[v, x] := '*'; end; end;
This corresponds to the array’s declaration.
It is vital to ensure the indices you are specifying are indeed valid.
In the latter loop the
if branch checks for that.
Attempting to access non-existent array values, i. e. by supplying illegal indices, may crash the program, or worse remain undetected thus causing difficult to find mistakes.
|A program compiled with the GPC will terminate with|
if you are attempting to access a non-existent array item (the final hexadecimal number may vary). The FPC, however, by default ignores this. You need to specifically request errors to be detected either by specifying the
./a.out: value out of range (error #300 at 402a76)
in your source code (before you are accessing any arrays, writing this once in your source code is enough).
Confer also chapter “Enumerations”, subsection “Restriction”.
Note, the “unusual” order of
y has been chosen to facilitate drawing an upright graph:
// print graph, note reverse `downto` direction for y := rowMaximum downto rowMinimum do begin writeLn(curve[y]); end; end.
That means, it is still possible to refer to entire “sub”-arrays as a whole. You are not forced to write all dimension an array value has, given it makes sense.
Array data types that have exactly two dimensions are also called matrices, singular matrix.
|In mathematics a matrix does not necessarily have to be homogenous, but could contain different “data types”.|
Real values[edit | edit source]
As introduced in one of the first chapters the data type
real is part of the Pascal programming language.
It is used to store integer values in combination with an optional fractional part.
Real literals[edit | edit source]
In order to distinguish
integer literals from
real literals, specifying
real values in your source code (and also for
readLn) differs slightly.
The following source code excerpt shows some
program realDemo; var x: real; begin x := 1.61803398; x := 1E9; x := 500e-12 x := -1.7320508; x := +0.0; end.
To summarize the rules:
realvalue literal always contains either a
.as a radix mark, or an
eto separate an exponent (the in ), or both.
- There is at least one Western-Arabic decimal digit before and one after the
.(if there is any).
- The entire number and exponent are preceded by signs, yet a positive sign is optional.
As it has always been, all number values cannot contain spaces.
Limitations[edit | edit source]
real data type has many limitations you have to be aware of in order to effectively use it.
First of all, we want to re-emphasize an issue that was mentioned when data types were introduced:
real variables can only store a subset of rational numbers (ℚ).
That means, for example, you cannot store the (mathematically speaking) real number (ℝ) .
This number is an irrational number (i. e. not a rational number).
If you cannot write a number as a finite
real literal, it is impossible to store it in a system using a finite amount of memory, such as computer systems do.
Fortunately, in EP three constants aide your usage of
minReal is the smallest positive
It conjunction with the constant
maxReal, it is guaranteed that all arithmetic operations in produce, quote, reasonable approximations.
It is not specified what exactly constitutes a “reasonable” approximation, thus it can, for example, mean that
maxReal - 1 yields as “an approximation”
Also, it is quite possible that
real variables can store larger values than
epsReal is short for “epsilon
The small Greek letter ε (epsilon) frequently denotes in mathematics an infinitely small (positive) value, yet not zero.
According to the ISO standard 10206 (“Extended Pascal”),
epsReal is the result of subtracting
1.0 from the smallest value that is greater than
No other value can be represented between this value and
epsReal represents the highest precision available, but just at that point.[fn 5]
Most implementations of the
real data type will show a significantly varying degree of precision.
Most notable, the precision of
real data type implementations complying with the IEEE standard 754 format, decays toward the extremes, when approaching (and going beyond)
Therefore you usually use, if at all, a reasonable multiple of
epsReal that fits the given situation.
Transfer functions[edit | edit source]
Pascal’s strong typing system prevents you from assigning
real values to
real value may contain a fractional part that an
integer variable cannot store.
Pascal defines, as part of the language, two standard functions addressing this issue in a well-defined manner.
- The function
trunc, short for “truncation”, simply discards any fractional part and returns, as an
integer, the integer part. As a consequence this is effectively rounding a number toward zero.
trunc(-1.999)will return the value
- If this feels “unnatural”, the
roundfunction rounds a
realnumber to its closest
round(x)is (regarding the resulting value) equivalent to
trunc(x + 0.5)for non-negative values, and equivalent to
trunc(x - 0.5)for negative
xvalues.[fn 6] In other words, this is what you were probably taught in grade school, or the first rounding method you learned in homeschooling. It is commonly referred to as “commercial rounding”.
Both functions will fail if there is no such
integer value fulfilling the function’s respective definition.
There is no function if you want to (explicitly) “transfer” an
integer value to a
Instead, one uses arithmetically neutral operations:
integerValue * 1.0(using multiplicative neutral element), or
integerValue + 0.0(using summing neutral element)
These expressions make use of the fact, as it was mentioned earlier as a passing remark in the chapter on expressions, that the expression’s overall data type will become
real as soon as one
real value is involved.
|It is not guaranteed that all possible |
real values[edit | edit source]
real values using “scientific notation”.
While this style is very universal, it may also be unusual for some readers.
E notation is something now rather archaic, usually only seen on pocket calculators, i. e. devices lacking of enough display space.
writeLn allow us to customize the displayed style.
writeLn accept for
real type values (and only for
real values) another colon-separated format specifier.
This second number determines the (exact) number of post-decimal digits, the “fraction part”.
Supplying two format specifiers also disables scientific notation.
real values are printed using regular positional notation.
That may mean for “large” numbers such as
1e100 printing a one followed by a hundred zeros (just for the integer part).
In some regions and languages it is customary to use a
, (comma) or other character instead of a dot as a radix mark.
writeLn procedures will, on the other hand, always print a dot, and for that matter –
readLn will always accept dots as radix marks only.
Nevertheless, all current Pascal programming suites, Delphi, FPC and GPC provide appropriate utilities to overcome this restriction.
For further details we refer to their manuals.
This issue should not keep us from continuing learning Pascal.
Comparisons[edit | edit source]
First of all, all (arithmetic) comparison operators do work with
<>, though, are particularly tricky to handle.
In most applications you do not compare
real values to each other when checking for equality or inequality.
The problem is that numbers such as ⅓ cannot be stored exactly with a finite series of binary digits, only approximated, yet there is not one valid approximation for ⅓ but many legit ones.
<> operators, however, compare – so to speak — for specific bit patterns.
This is usually not desired (for values that cannot be represented exactly).
Instead, you want to ensure the values you are comparing are within a certain range, like:
function equal(x, y, delta: real): Boolean; begin equal := abs(x - y) <= delta; end;
Delphi and the FPC’s standard RTS provide the function
sameValue as part of the
You do not want to program something other people already have programmed for you, i. e. use the resources.
Division[edit | edit source]
Now that we know the data type used for storing (a subset of) rational numbers, in Pascal known as
real, we can perform and use the result of another arithmetic operation:
Flavors[edit | edit source]
Pascal defines two different division operators:
divoperator performs an integer division and discards, if applicable, any remainder. The expression’s resulting data type is always
divoperator only works if both operands are
- The, probably more familiar, operator
/(a forward slash), divides the LHS number (the dividend) by the RHS number (the divisor), too, but a
/-division always yields a
realtype value. This is also the case if the fractional part is zero. A “remainder” does not exist for the
div operation can be put in terms of
divisor div dividend = trunc(divisor / dividend)
However, this is only a semantic equivalent,[fn 8] it is not how it is actually calculated.
The reason is, the
/ operator would first convert both operands to
real values and since, as explained above, not all
integer values can necessarily be represented exactly as
real values, this would produce results potentially suffering from rounding imprecisions.
div operator on the other hand is a pure
integer operator and works with “integer precision”, that means no rounding is involved in actually calculating the
Off limits divisor[edit | edit source]
Note, since there is no generally accepted definition for division by zero, a zero divisor is illegal and will result in your program to abort. If your divisor is not a constant and depends on run-time data (such as a variable read from user input), you should check that it is non-zero before doing a division:
if divisor <> 0 then begin x := dividend / divisor; // ... end else begin writeLn('Error: zero divisor encountered when calculating rainbow'); end;
Alternatively, you can declare data types excluding zero, so any assignment of a zero value will be detected:
type natural = 1..maxInt; var divisor: natural; begin write('Enter a positive integer: '); readLn(divisor); // entering a non-positive value will fail
Some Pascal dialects introduce the concept of “exceptions” that can be used to identify such problems. Exceptions may be mentioned again in the “Extensions” part of this Wikibook.
Arrays as parameters[edit | edit source]
Arrays can be copied with one simple assignment
dataOut := dataIn;.
This requires, however, as it is customary with Pascal’s strong type safety, that both arrays are assignment-compatible:
That means their base type and dimension specifications are the same.[fn 9]
Because calling a routine involves invisible assignments, writing general-purpose code dealing with lots of different situations would be virtually impossible if the entire program had to use one array type only. In order to mitigate this situation, conformant array type parameters allow writing routines accepting differing array dimension lengths. Array dimension data types still have to match.
Let’s look at an example program using this feature:
program tableDemo(output); procedure printTableRows(data: array[minimum..maximum: integer] of integer); var i: integer; // or in EP `i: type of minimum;` [preferred alternative] begin for i := minimum to maximum do begin writeLn(i:20, ' | ', data[i]:20); end; end;
A conformant-array parameter looks pretty similar to a regular array variable declaration, but the dimensions are specified differently.
Usually, when declaring new arrays you provide constant values as dimension limits.
Since we do not want constants, though, we name placeholder identifiers for the actual dimension limits of any array
printTableRows will receive.
In this case they are named
maximum, joined by
.. inbetween indicating a range.
maximum become variables inside the definition of
printTableRows, but you are not allowed to assign any values to them.[fn 10]
You are not allowed to declare new identifiers bearing the same name as the array boundary variables.
In Pascal all constants implicitly have an unambiguously determinable data type.
Since our array limit identifiers are in fact variables they require a data type.
: integer indicates both
maximum have the data type
|In a conformant array paramter, the short notation for nested arrays uses a |
Once we have declared and defined
printTableRows we can use it with lots of differently sized arrays:
var table: array[0..3] of integer; nein: array[9..9] of integer; begin table := 1; table := 2; table := 4; table := 8; printTableRows(table); nein := 9; printTableRows(nein); end.
Delphi and the FPC (as of version 3.2.0 released in 2020) do not support conformant-array parameters, but the GPC does.
Logarithms[edit | edit source]
Special support[edit | edit source]
Prior the 21st century logarithm tables and slide rules were heavily utilized tools in manual calculations, so much so it led to the inclusion of two basic functions in Pascal.
- The function
expexponentiates a number to the base , Euler’s constant. The value of
exp(x)is equivalent to the mathematical term .
- The function
ln, short for Latin “logaritmus naturalis”, takes the natural logarithm of a positive number. “Natural” refers to again.
Both functions always return
Introduction[edit | edit source]
Since the use of logarithms is not necessarily taught in all curricula, or you might just need a refresher, here is a short primer: The basic idea of logarithms is that all operations are lifted one level.
On the lifted level many operations become simpler, especially if the numbers are large. For instance, you can perform a rather easy addition if you actually mean to take the product of two numbers. For this you have to “lift” all operands one level up: This is done by taking the logarithm. Pay particular attention to : On the logarithm level is a non-“logarithmized” factor, you only take the logarithm of
Once you are done, you have descend one level again to get the actual “real” result of the intended operation (on the underlying level).
The reverse operation of
To put this principle in Pascal terms:
x * y = exp(ln(x) + ln(y))
y have to be positive in order to be valid parameters to
Application[edit | edit source]
Taking the logarithm and then exponentiating values again are considered “slow” operations and introduce a certain overhead. In programming, overhead means taking steps that are not directly related to the actual underlying problem, but only facilitate solving it. In general, overhead is avoided, since (at first) it takes us even farther away from the solution.
As you can see, this goes to the detriment of precision. It is a compromise between fast operations, and “accurate enough” results.
The best solution is, of course, finding a better algorithm.
The above demonstration is effectively , that is
abs(x) (remember, squaring a number always yields a non-negative number).
This operation’s result will stay in the range of
Tasks[edit | edit source]
All tasks, including those in the following chapters, can be solved without conformant-array parameters. This takes account of the fact that not all major compilers support them.[fn 12] Nonetheless, using routines with conformant-array parameters are often the most elegant solution.
More tasks you can solve can be found on the following Wikibook pages:
- A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/One-Dimensional Arrays
- A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Two-Dimensional Arrays
- Jensen, Kathleen; Wirth, Niklaus. Pascal – user manual and report (4th revised ed.). p. 56. doi:10.1007/978-1-4612-4450-9. ISBN 978-0-387-97649-5. "An array type consists of a fixed number of components (defined when the array type is introduced) all having the same type, called the component type."
- This limitation comes from Pascal: ISO 7185:1990 (Report). ISO/IEC. 1991. p. 16. "real-type. The required type-identifier real shall denote the real-type. […] The values shall be an implementation-defined subset of the real numbers, denoted as specified in 6.1.5 by signed-real." The end of the last sentence implies only writable, those you can specify in your source code, are legal
realvalues. For example, the value is different from , the decimial representation of which would be infinitely long (a. k. a. irrational number), thus the actual, correct value of cannot appear in source code as a “
- Joslin, David A. (1989-06-01). "Extended Pascal – Numerical Features". Sigplan Notices 24 (6): 77–80. doi:10.1145/71052.71063. "The programmer can obtain some idea of the real range and precision from the (positive) implementation-defined constants
EPSREAL. Arithmetic in the ranges
[minreal,maxreal]“can be expected to work with reasonable approximations”, whereas outside those ranges it cannot. As what constitutes a “reasonable approximation” is a matter of opinion, however, and is not defined in the standard, this statement may be less useful than it appears at first sight. The measure of precision is on somewhat firmer ground:
EPSREALis the commonly employed measure of (typically floating-point) accuracy, i.e the smallest value such that
1.0 + epsreal > 1.0.".
- Jensen, Kathleen; Wirth, Niklaus. Pascal – user manual and report (4th revised ed.). p. 20. doi:10.1007/978-1-4612-4450-9. ISBN 978-0-387-97649-5. "As long as at least one of the operands is of type
Real(the other possibly being of type
Integer) the following operators yield a real value:
divide (both operands may be integers, but the result is always real)
- Some (old) computers did not know the bracket characters. Seriously, that’s not a joke. Instead, a substitute bigram was used:
var signCharacter: array(.Boolean.) of char;, and
signCharacter(.true.) := '-';. You may encounter this kind of notation in some (old) textbooks on Pascal. Anyway, using brackets is still the preferred method.
- More precisely,
textfiles are (possibly empty) sequences of lines, each line consisting of a (possibly empty) sequence of
- Some compilers (such as the FPC) allow zero-sized data types [not allowed in any ISO standard]. If that is the case, an array that has a zero-size base type will be rendered ineffective, virtually forfeiting all characteristics of arrays.
realarithmetic processors can indicate a precision loss, i. e. when the result of an arithmetic operation had to be “approximated”. However, there is no standardized way to access this kind of information from your Pascal source code, and usually this kind of signaling is also not favorable, since the tiniest precision loss will set off the alarm, thus slowing down your program. Instead, if it matters, one uses software that allows arbitrary precision arithmetics, like for example the GNU Multiple Precision Arithmetic Library.
- This number is not completely arbitrary. The most prevalent
realnumber implementation IEEE 754 uses a “hidden bit”, making the value
- Not all compilers comply with this definition of the standard. The FPC’s standard
roundimplementation will round in the case of equidistance toward even numbers. Knowing this is relevant for statistical applications. The GPC uses for its
roundimplementation functionality provided by the hardware. As such, the implementation is hardware-dependent, on its specific configuration, and may deviate from the ISO 7185 standard definition.
- Given the most prevalent implementations Two’s complement for
integervalues and IEEE 754 for
realvalues, you have to consider the fact that (virtually) all bits in an
integercontribute to its (mathematical) value, whereas a
realnumber stores values for the expression
mantissa * base pow exponent. In very simple terms, the
integerpart of a value, but the problem is that it occupies fewer bits than an
integerwould use, thus there is (for values that require more bits) a loss in information (i. e. a loss in precision).
- The exact technical definition reads like: The value of
dividend div divisorshall be such thatwhere the value shall be zero if
abs(dividend) - abs(divisor) < abs((dividend div divisor) * divisor) <= abs(dividend)
abs(dividend) < abs(divisor); otherwise, […]
- Furthermore, both arrays have to be either
- The EP standard calls this characteristic
- It is important that both functions use one common base, in this case it is .
- The ISO standard 7185 (“Standard Pascal”) calls this, lack of conformant-array parameters, “Level 0 compliance”. “Level 1 compliance” includes support for conformant array parameters. Of the compilers presented in Getting started only the GPC is a “Level 1”-compliant compiler.
- This style of programming is slightly disfavored, keyword “global variables”, but as for now we do not know appropriate syntax (
varparameters) to not do that.
- For extra credit: You can make use of the fact that (assuming that
zMaximumis positive) . You can use this value to find out the minimum number of digits required.
- In EP there also exists a
**. The difference is similar to that of the division operators:
integervalues as operands and yields an
**always yields a
realvalue. Your choice for either of which, again, should be based on the required degree of precision.