Appendix A: Review of Data Types in Pascal
Appendix A: Review of Data Types in Pascal
Appendix A: Review of Data Types in Pascal
Pascal
There are four simple scalar data types in Pascal: INTEGER, REAL, CHAR
and BOOLEAN.
Arithmetic operators+,-,* are defmed for real and integers data types,
with the division operators MOD, DIV (integer division) and I (real division).
Standard functions such as SIN, COS, EXP are also provided.
Variables of type CHAR hold a single character value, while Booleans store
a value TRUE or FALSE.
Comparison operators allow variables or expressions of compatible types to
be compared (for=,<>,<, etc.) to yield a Boolean result. Boolean operators
AND, OR, NOT are available to test more complex conditions.
The assignment statement can be used to evaluate an expression involving
variables, constants and operators, and store the result in a variable of the
appropriate type. Type-checking is performed on all operations and assignments,
ensuring that only compatible data types are used. Integer expressions can be
assigned to integer or real variables, but real expressions can be assigned only
to real variables. Boolean expressions can be assigned to Boolean variables while
character expressions can be assigned to CHAR variables.
The simple data types can be used to build new data types. In Pascal there are
the subrange and enumeration types.
(a) Subranges
249
250 Data Structures. Files and Databases
VAR
( * declare variables of these types *)
i: posinteger;
cap : capitals;
declares the variable i to be a positive integer which is a subrange of the
INTEGER data type, and the variable cap to be a capital letter which is a sub-
range of the data type CHAR.
The main advantages of using sub ranges are that they show clearly the
intended values of the variables and permit more error detection. The internal
representation of the subrange may be the same as for the full range, or it may
differ. Data items of these types can be used in operations with values from
the full range, and any necessary conversions will be done automatically.
The data types INTEGER, CHAR and enumeration types all share the property
that there is an underlying order to the values which allows you to determine
the successor or predecessor of any particular value. This property is not shared
by the data type REAL. The types sharing the property are called ordinal types
and are the only ones which can be used as the base type for subranges. Although
there may be situations when you would like to use a subrange of REAL values,
such a type cannot be defined in Pascal.
Sets
In Pascal, a type can be defined as a set of another base type. For instance
(assuming the declarations as above), in the statement
IF cap IN [ 'A' .. 'C' , 'L'] THEN ...
Appendix A: Review of Data Types in Pascal 251
the set constant [ 'A' .. 'C' , 'L' ] is used in the test to see whether cap is any
one of the values 'A', 'B', 'C' or 'L'.
The similar statement
IF i IN [ 'A' .. 'C' , 'L' ] THEN ...
would be trapped as an error because the variable i is not of the correct type to
be compared with the characters in the set.
The base type of the set must be a subrange of an ordinal type, and may be
restricted in the number of elements allowed.
Set variables can be declared, and the operators+,* and- used for set union,
intersection and difference. Figure A.l shows a set variable ACCEPTED being
declared as a set whose base type is the letters A to Z. It is used in the code to
build up the set of characters found on one line of text.
(* declarations *)
VAR Accepted: SET OF 'A' .. 'Z':
(* code *)
Accepted:=(]: (*initialise to empty set*)
WHILE NOT EOLN DO
BEGIN
READ(ch):
IF chIN ['A' .. 'Z'] THEN
BEGIN
IF ch IN Accepted
THEN
WRITELN(' Character already given ')
ELSE
BEGIN
Accepted:=Accepted + [ch]:
(* add ch to Accepted set *)
WRITELN(' New character accepted')
END:
END
END:
One-dimensional a"ays
A one-dimensional array is a block of elements, all of the same base type, each
value being identified by a subscript value. The subscript range must be from
an ordinal type such as 1 .. 20, -60 .. 43, or 'A' .. 'Z', and is specified as part
of the array declaration.
252 Data Structures, Files and Databases
Processing strings
Individual characters can be accessed as elements of such an array, while the full
string can be processed by referring to the array name without subscripts. With
the declarations
VAR name I , name2 : String20;
the assignment
namel:=name2;
can be used.
Furthermore, comparisons between strings are allowed, testing for equality,
inequality, or any of the order relations which check for alphabetic order, as in
IF (name I = name2) THEN .. .
IF (name I< name2) THEN .. .
Appendix A: Review of Data Types in Pascal 253
Higher-dimensional arrays
Representing matrices
A two-dimensional array (or matrix) is a grid of elements of the same base type.
Each element needs two subscripts to identify it. Often these two subscripts can
be regarded as the element's row and column position. We shall treat the first
subscript as the row position, the second as the column position.
Processing matrices
As with the one-dimensional case, the array can be processed element by element,
or treated as a single unit in an assignment. In addition, it is possible to consider
rows or columns as vectors in their own right. In Pascal, you can treat each row
as a vector and perform assignments to vectors of the same type. But columns
cannot be treated similarly. Figure A.2 shows an example where two rows of a
matrix are interchanged. It illustrates how Pascal regards a two-dimensional
array as a one-dimensional array of rows which are themselves vectors.
(* declarations *)
TYPE
vector ARRAY [1 •• 3) OF REAL;
matrix ARRAY [1 .• 6] OF vector;
VAR
M: matrix; (* 6 rows of 3 entries *)
c: vector; (* vector of 3 entries *)
i,j : 1..6; (* subscript for rows *)
BEGIN
.
(* swap row i with row j *)
c:= M[i); (* save row i *)
M[i] := M[j]; (* copy ro~ ~ to row i *)
M[j):= c; (* copy or1g1nal row i to row j *)
Records
Abstract view
Often a collection of data items, usually containing different values about the
same entity, are grouped together into a single unit called a record. The individual
items of data are called the fields of the record. For example, customer details
may be held in a record containing:
The individual fields may be of the same or different types. Some processing
will look at the fields individually, while other processing will treat the whole
record as a unit.
Concrete representation and processing
In Pascal, the record's fields are defined in its type declaration, while the
records themselves are declared as variables of that type. In figure A.3 the
records are called CustomerA, CustomerB and CustomerC. The individual
fields are referred to as CustomerA.account, CustomerB.account, etc. The
WITH statement is used to allow one record name to qualify several references
to its fields without its name having to be written in front of each field name.
Where the whole record is accessed as a unit, only the record name is speci-
fied.
Appendix A: Review of Data Types in Pascal 255
TYPE
(* declare record type *)
CustomerDetail =
RECORD
account INTEGER;
name PACKED ARRAY [1 .. 15] OF CHAR;
address PACKED ARRAY [1 •• 40) OF CHAR;
tel No PACKED ARRAY [1 •• 12] OF CHAR
END;
VAR
(* declare three records of this type *)
CustomerA, customers, CustomerC : customerDetail;
BEGIN
(* read values into fields of CustomerA record
one by one *)
WITH CustomerA DO
READLN( account, name, address, telNo);
(* repeat for CustomerS *)
WITH CustomerS DO
READLN( account, name, address, telNo);
(* compare account fields of records *)
IF (CustomerA.account = CustomerB.account) THEN
WRITELN(' Same account numbers ')
ELSE
IF (CustomerA.account > CustomerB.account) THEN
BEGIN (* swap the records *)
Customerc .- CustomerA;
(* save first record *)
CustomerA := CustomerS;
(* copy second record to first *)
Customers := Customerc;
(* copy original first record to second *)
END;
END.
The exact format of the end-of-line markers varies from machine to machine.
Programming language features can isolate the programmer from these details
by providing ways of writing end-of-line markers, detecting them and skipping
over them. The programmer must be aware that special markers are there, but
need not know exactly what the markers are.
In Pascal, the two files INPUT and OUTPUT are pre-declared text files. Other
text files can be declared as variables of the file type TEXT, as in
VAR reportFile : TEXT;
Some Pascal implementations require the names of all files used in a program
to be declared in the program header. A program using three files could use a
program header statement of the form
PROGRAM Sales (INPUT ,OUTPUT ,report File);
The file variable name is used inside the program to identify the file. The
correspondence between this name and the actual file is done by the operating
system in a manner which depends on the system.
A file being used for input must be initialised by the RESET statement, while
one being used for output must be initialised by the REWRITE statement. The
two pre-declared files are already initialised. The procedures READ and WRITE
can be used to process text files. The first parameter to these procedures is the
file variable. If omitted, the file INPUT or OUTPUT is assumed.
256
Appendix B: Text Files in Pascal 257
(* declarations *)
VAR infile, outfile TEXT:
ch : CHAR:
X : REAL:
i : INTEGER:
(* program body *)
BEGIN
RESET ( infile) :
(* prepare file for input *)
REWRITE (outfile):
(* prepare file for output *)
READ (infile, ch):
(* read a single character *)
READ (infile, x,i):
(* read a real value and an integer *)
WRITE (outfile, 'Character was •,ch,
' and numbers were •,x,' and ',i):
(* write text and values to output file *)
Figure 8.2 shows how individual characters, real and integer values can be
handled by the READ and WRITE procedures. Real and integer data types are
said to be compatible with the me component type CHAR. Only values of
compatible data types can be read from or written to a text file. The appropriate
conversions are carried out as part of the READ or WRITE procedures.
The two Boolean functions, EOF and EOLN, can be used to detect the
end-of-file and end-of-line conditions. They return a value TRUE when the f:tle
is positioned at the end of f:tle or end of a line. READLN will skip over an
end-of-line marker, skipping any preceding characters still unread, while
WRITELN will write an end-of-line marker to the output f:tle.
Figure 8.3 is an example program which processes a text file character by
character. It reads an input text ftle and copies it to an output text ftle, including
line numbers on each line. Note that although the example uses its own me
variable names, the files INPUT and OUTPUT could have been used instead.
258 Data Structures, Files and Databases
The following texts cover the features of Pascal including pointer data types
and recursion.
W. Findlay, and D. A. Watt, Pascal, An Introduction to Methodical Programming,
2nd edition, Computer Science Press, 1981.
D. M. Munro, A Crash Course in Pascal, Edward Arnold, 1985.
I. R. Wilson and A.M. Addyman,A Practical Introduction to Pascal- with
BS6I92, 2nd edition, Macmillan Computer Science Series, 1982.
The references on data structures include texts which give a fuller exposition
of the material covered in this text, including the topic of AVL-trees referred
to in chapter 6.
A. V. Aho, J. E. Hopcroft and J.D. Ullman, Data Structures and Algorithms,
Addison-Wesley, 1983.
E. Horowitz and S. Sahni, Fundamentals of Data Structures, Computer Science
Press, 1975.
D. E. Knuth, The Art of Computer Programming, Vol. I: Fundamental Algorithms
2nd edition, Addison-Wesley, 1973.
D. Kruse, Data Structures and Program Design, Prentice-Hall, 1984.
A.M. Tenenbaum and M. J. Augenstein, Data Structures using Pascal, 2nd
edition, Prentice-Hall, 1986.
N. Wirth, Algorithms+ Data Structures= Programs, Prentice-Hall, 1976.
259
260 Data Structures, Files and Databases
Many of the above references cover the program design aspects of data struc-
tures. In addition, we give three other references.
D. Coleman, A Structured Programming Approach to Data, Macmillan Computer
Science Series, 1978.
M. Jackson, Principles of Program Design; Academic Press, 197 5.
M. J. King and J.P. Pardoe, Program Design UsingJSP- A Practical Introduction,
Macmillan Computer Science Series, 1985.
Texts dealing with formal specifications and the role of abstract data types
using different approaches are: