Pascal Programming/Arrays
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
Notion
Template:Wikibook 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.[1] 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
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
A variable of the data type integerList as declared above, holds five independent integer values.
Accessing them follows a specific scheme:
var
powerN: integerList;
begin
powerN[1] := 5;
powerN[2] := 25;
powerN[3] := 125;
powerN[4] := 625;
powerN[5] := 3125;
writeLn(powerN[3]);
end.
This program will print 125, since it is the value of powerN that has the index value 3.
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
Lists of characters frequently have and had special support with respect to Template:Abbr and manipulation.
This section is primarily about understanding, as in the [[../Strings|next chapter]] we will get to know a more sophisticated data type called string.
Direct assignment
String literals can be assigned to Template:Nowrap 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[1] := 'A';
currentFileReference[2] := 'Z';
currentFileReference[3] := '0';
currentFileReference[4] := '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 Template:Abbr 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 Template:Abbr uses space characters (Template:Nowrap), whereas the Template:Abbr uses char values whose ordinal value is zero (#0).
Reading and printing
Although not standardized,[fn 2] read/readLn and write/writeLn usually support writing to and reading from Template:Nowrap variables.
This works because text files, like the standard files input and output, are understood to be infinite sequences of char values.[fn 3]
Since our Template:Nowrap 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
Unlike other arrays, Template:Nowrap variables can be compared using = and <>.
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.
Correspondingly, an <> comparison will always succeed for Template:Nowrap values that differ in length.
Note, the Template:Abbr standard also defines the Template:Abbr and Template:Abbr functions, beside many more.
The difference to = and <> is that blank padding (i. e. #0 in FPC or Template:Nowrap in GPC) has no significance in EQ and NE.
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.
The = and <> comparisons on the other hand look at the memory’s internal representation.
Matrices
An array’s base type can be any data type,[fn 4] 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 plot uses 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.
Template:XNote
Note, the “unusual” order of x and 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. Template:XNote
Real values
As [[../Variables and Constants#Data type|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
In order to distinguish integer literals from real literals, specifying real values in your source code (and also for read/readLn) differs slightly.
The following source code excerpt shows some real literals:
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:
- A
realvalue literal always contains either a.as a radix mark, or anE/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.
Template:XNote As it has always been, all number values cannot contain spaces.
Limitations
The 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 [[../Variables and Constants#Data type|mentioned when data types were introduced]]:
In computing real variables can only store a subset of rational numbers (ℚ).[2]
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 Template:Abbr three constants aide your usage of real values.
minReal is the smallest positive real value.
It conjunction with the constant maxReal, it is guaranteed that all arithmetic operations in produce, quote, reasonable approximations.[3]
It is not specified what exactly constitutes a “reasonable” approximation, thus it can, for example, mean that Template:Nowrap yields as “an approximation” maxReal.[fn 5]
Also, it is quite possible that real variables can store larger values than maxReal.
Template:Wikipedia
epsReal is short for “epsilon real”.
The small Greek letter ε (epsilon) frequently denotes in mathematics an infinitely small (positive) value, yet not zero.
According to the Template:Abbr standard 10206 (“Extended Pascal”), epsReal is the result of subtracting 1.0 from the smallest value that is greater than 1.0.[3]
No other value can be represented between this value and 1.0, thus epsReal represents the highest precision available, but just at that point.[fn 6]
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 Template:Abbr standard 754 format, decays toward the extremes, when approaching (and going beyond) -maxReal and maxReal.
Therefore you usually use, if at all, a reasonable multiple of epsReal that fits the given situation.
Transfer functions
Pascal’s strong typing system prevents you from assigning real values to integer variables.
The 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 aninteger, the integer part. As a consequence this is effectively rounding a number toward zero.trunc(-1.999)will return the value-1. - If this feels “unnatural”, the
roundfunction rounds arealnumber to its closestintegervalue.round(x)is (regarding the resulting value) equivalent to Template:Nowrap for non-negative values, and equivalent to Template:Nowrap for negativexvalues.[fn 7] 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 real value.
Instead, one uses arithmetically neutral operations:
- Template:Nowrap (using multiplicative neutral element), or
- Template:Nowrap (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 and Branches#Calculations|expressions]], that the expression’s overall data type will become real as soon as one real value is involved.[4]
Template:XNote
Printing real values
By default write/writeLn print real values using “scientific notation”.
Template:Code:Output
While this style is very universal, it may also be unusual for some readers.
Particularly the E notation is something now rather archaic, usually only seen on pocket calculators, i. e. devices lacking of enough display space.
Luckily, write/writeLn allow us to customize the displayed style.
Template:Code:Output
The procedures write/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.
All 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).
Template:Code:Output
In some regions and languages it is customary to use a , (comma) or other character instead of a dot as a radix mark.
Pascal’s on-board write/writeLn procedures will, on the other hand, always print a dot, and for that matter – read/readLn will always accept dots as radix marks only.
Nevertheless, all current Pascal programming suites, Delphi, Template:Abbr and Template:Abbr 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
First of all, all (arithmetic) comparison operators do work with real values.
The operators = and <>, 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.
The = and <> 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 Template:Abbr’s standard Template:Abbr provide the function sameValue as part of the math [[../Units|unit]].
You do not want to program something other people already have programmed for you, i. e. use the resources.
Division
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:
The division.
Flavors
Pascal defines two different division operators:
- The
divoperator performs an integer division and discards, if applicable, any remainder. The expression’s resulting data type is alwaysinteger. Thedivoperator only works if both operands areintegerexpressions. - The, probably more familiar, operator
/(a forward slash), divides the Template:Abbr number (the dividend) by the Template:Abbr number (the divisor), too, but a/-division always yields arealtype value.[4] This is also the case if the fractional part is zero. A “remainder” does not exist for the/operation.
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.
The 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 div result.
Off limits divisor
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 failSome 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
Arrays can be copied with one simple assignment Template:Nowrap. 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 minimum and maximum, joined by .. inbetween indicating a range.
minimum and 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.
The Template:Nowrap indicates both minimum and maximum have the data type integer.
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[0] := 1;
table[1] := 2;
table[2] := 4;
table[3] := 8;
printTableRows(table);
nein[9] := 9;
printTableRows(nein);
end.Delphi and the Template:Abbr (as of version 3.2.0 released in 2020) do not support conformant-array parameters, but the Template:Abbr does.
Logarithms
Special support
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 ofexp(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 real values.
Introduction
Template:Wikibook 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.
| logarithm level | |||
|---|---|---|---|
| real 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 ln is exp.[fn 11]
To put this principle in Pascal terms:
x * y = exp(ln(x) + ln(y))Remember, x and y have to be positive in order to be valid parameters to ln.
Application
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. Template:Code:Output 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 real.
Tasks
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. Template:Question-answer Template:- Template:Question-answer Template:- Template:Question-answer Template:- Template:Question-answer Template:- Template:Question-answer Template:- 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
Sources:
- ↑ Template:Cite book
- ↑ This limitation comes from Template:Cite report 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 “real” value. - ↑ 3.0 3.1 Template:Cite journal
- ↑ 4.0 4.1 Template:Cite book
Notes:
- ↑ Some (old) computers did not know the bracket characters. Seriously, that’s not a joke. Instead, a substitute bigram was used: Template:Nowrap, and Template:Nowrap. You may encounter this kind of notation in some (old) textbooks on Pascal. Anyway, using brackets is still the preferred method.
- ↑ Only Template:Abbr concerning a Template:Nowrap, where
nis greater than1andcomponentTypeis or is a subrange ofchar, is standardized. However, in this part of the book you are not introduced to the concept of packing, thepackedkeyword. Therefore, the shown behavior is non-standard. - ↑ More precisely,
textfiles are (possibly empty) sequences of lines, each line consisting of a (possibly empty) sequence ofcharvalues. - ↑ Some compilers (such as the Template:Abbr) 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.
- ↑ Modern
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 value1.0special. - ↑ Not all compilers comply with this definition of the standard. The Template:Abbr’s standard
roundimplementation will round in the case of equidistance toward even numbers. Knowing this is relevant for statistical applications. The Template:Abbr uses for itsroundimplementation 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. - ↑ The exact technical definition reads like: The value of Template:Nowrap shall be such thatwhere the value shall be zero if Template:Nowrap; otherwise, […]
abs(dividend) - abs(divisor) < abs((dividend div divisor) * divisor) <= abs(dividend)
- ↑ Furthermore, both arrays have to be either
packedor “unpacked”. - ↑ The Template:Abbr standard calls this characteristic
protected. - ↑ It is important that both functions use one common base, in this case it is .
- ↑ The Template:Abbr 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|Getting started]] only the Template:Abbr is a “Level 1”-compliant compiler.