CSC 204 Session 4
CSC 204 Session 4
Introduction
In data processing, composite data types such as descriptions of persons or objects usually occur in
files or data banks to record the relevant characteristics of a person or object. The word record has
therefore become widely accepted to describe a compound of data of this nature.
In this study session, you will be introduced to the definition of records, its creation, declaration, its
representation and the use of records in computer programming.
Page 70 of 269
CSC 204: Fundamentals of Data Structures
For example, a date could be stored as a record containing a numeric year field, a month field
represented as a string, and a numeric day-of-month field. As another example, a Personnel record
might contain a name, a salary, and a rank.
Records can exist in any storage medium, including main memory and mass storage devices such as
magnetic tapes or hard disks. Records are a fundamental component of most data structures,
especially linked data structures. Many computer files are organized as arrays of logical records,
often grouped into larger physical records or blocks for efficiency.
A record is a special type of data structure that, unlike arrays, collects different data types that
define a particular structure such a book, product, person and many others.
A record structureis an aggregate entity containing one or more elements.
Page 71 of 269
CSC 204: Fundamentals of Data Structures
hardware support for delimiting records and fields, and special instructions for copying such
records.
COBOL was the first widespread programming language to support record types, and its record
definition facilities were quite sophisticated at the time.
The Pascal programming language was one of the first languages to fully integrate record types
with other basic types into a logically consistent type system. Most languages designed after Pascal
(such as Ada, Modula, C, C# and Java) also supported records.
Page 72 of 269
CSC 204: Fundamentals of Data Structures
Example 1
type
record_type_name =record
field_name_1: field_type_1;
field_name_2: field_type_2;
end record;
Type
biodata=Record
name => “Mrs A.A Akpors”;
phone => “08022334455”;
sex => female;
age => 21;
weight =>62.0; Page 73 of 269
End;
CSC 204: Fundamentals of Data Structures
Example 2
Type
persons = Record
name : string;
phone : String;
weight : Real;
age : int;
sex : char;
End;
As you might have noticed, the declaration of typical record data structure starts with the keyword
Record and always ends with the end keyword.
Page 74 of 269
CSC 204: Fundamentals of Data Structures
Example 3
A Pascal program will be used to compute an array of records to store a number of different books
and by using this example, it will be immensely indicative to learn how to use them.
In this example, you will be able to store 10 different books from input and then output only one
chosen record to display it back to screen in order to demonstrate how to access a record from an
array.
Type
Str24 = String[24];
Book_Rec = Record
Title : Str24;
Author : Str24;
ISBN : Str24;
Price : Real;
End;
Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Page 75 of 269
CSC 204: Fundamentals of Data Structures
Write('Price: ');
Readln(newBook.Price);
End;
Var
bookRecArray : Array[1..10] of TBookRec;
i : 1..10;
Begin
For i := 1 to 10 do
EnterNewBook(bookRecArray[i]);
Writeln('Thanks for entering the book details');
Write('Now choose a record to display from 1 to 10: ');
Readln(i);
Writeln('Here are the book details of record #',i,':');
Writeln;
Writeln('Title: ', bookRecArray[i].Title);
Writeln('Author: ', bookRecArray[i].Author);
Writeln('ISBN: ', bookRecArray[i].ISBN);
Writeln('Price: ', bookRecArray[i].Price);
Readln;
End.
Page 76 of 269
CSC 204: Fundamentals of Data Structures
Page 77 of 269
CSC 204: Fundamentals of Data Structures
ki = S1 + S2 + ... + Si-1 k0 = 0
We now realize that the fact that all components of an array are of equal type has the welcome
consequence that ki = i×s. The generality of the record structure does unfortunately not allow such
a simple, linear function for offset address computation, and it is therefore the very reason for the
requirement that record components be selectable only by fixed identifiers.
This restriction has the desirable benefit that the respective offsets are known at compile time. The
resulting greater efficiency of record field access is well-known.
The technique of packing may be beneficial, if several record components can be fitted into a single
storage word (see Figure 4.4). Since offsets are computable by the compiler, the offset of a field
packed within a word may also be determined by the compiler. This means that on many
computers, packing of records causes deterioration in access efficiency considerably smaller than
those caused by the packing of arrays.
Page 78 of 269
CSC 204: Fundamentals of Data Structures
The representation of sets by their characteristic function has the advantage that the operations of
computing the union, intersection, and difference of two sets may be implemented as elementary
logical operations. The following equivalences, which hold for all elements i of the base type of the
sets x and y, relate logical operations with operations on sets:
i IN (x+y) = (i IN x) OR (i IN y)
i IN (x*y) = (i IN x) & (i IN y)
i IN (x-y) = (i IN x) & ~(i IN y)
These logical operations are available on all digital computers, and moreover they operate
concurrently on all corresponding elements (bits) of a word. It therefore appears that in order to be
able to implement the basic set operations in an efficient manner, sets must be represented in a
small, fixed number of words upon which not only the basic logical operations, but also those of
shifting are available.
Testing for membership is then implemented by a single shift and a subsequent (sign) bit test
operation. As a consequence, a test of the form x IN {c1, c2, ... , cn} can be implemented
considerably more efficiently than the equivalent Boolean expression
Page 79 of 269
CSC 204: Fundamentals of Data Structures
A corollary is that the set structure should be used only for small integers as elements, the largest
one being the word length of the underlying computer.
Page 80 of 269
CSC 204: Fundamentals of Data Structures
1. Records (also called tuples or compound data) are among the simplest Data
Structures. A record is a value that contains other values, typically in fixed
number and sequence and typically indexed by names. The elements of records
are usually called fields or members.
2. Array is a homogeneous collection of components; all components of the same
kind, while a record is an heterogeneous collection of components.
3. Arrays of records are arrays whose elements are records. Records may be stored
in arrays and this will become very useful and it is not that difficult to manage.
4. A representation of an array structure is a mapping of the (abstract) array with
components of type T onto the store which is an array with components of type
BYTE.
5. Records are mapped onto a computer store by simply juxtaposing their
components.
6. A set s is conveniently represented in a computer store by its characteristic
function C(s). This is an array of logical values whose ith component has the
meaning “i is present in s”.
Page 81 of 269
CSC 204: Fundamentals of Data Structures
Pilot Answers
Pilot Answer 4.1
i. Arrays: A & C because they contain elements of the same kind
Records: B, D because they contain elements of different kinds
ii. The programming languages that support Records are: COBOL, Ada, Modula, C, C# and
Java
iii. A record is a value that contains other values, typically in fixed number and sequence
and typically indexed by names. The elements of records are usually called fields or
members.
type
record_type_name =record
field_name_1: field_type_1;
field_name_2: field_type_2;
end record;
ii. A record is a value that contains other values, typically in fixed number and sequence
and typically indexed by names. The elements of records are usually called fields or
members. While Array is a homogeneous collection of components; all components of
the same kind, a record is an heterogeneous collection of components.
Page 82 of 269
CSC 204: Fundamentals of Data Structures
mapped in such a way that the computation of addresses of array components is as simple (and
therefore as efficient) as possible.
Page 83 of 269
CSC 204: Fundamentals of Data Structures
Glossary of Terms
Record: A record is a value that contains other values, typically in fixed number and sequence and
typically indexed by names.
Record structure:A record structureis an aggregate entity containing one or more elements.
Arrays of records: Arrays of records are arrays whose elements are records. Records may be
stored in arrays and this will become very useful and it is not that difficult to manage.
Page 84 of 269
CSC 204: Fundamentals of Data Structures
i. Define Record
ii. Write out the declaration of record data type format
iii. Differentiate between an Array and a Record
iv. Explain the representation of arrays
Page 85 of 269