CSC 204_Study Session 4
CSC 204_Study 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 examine the definition of a string, as well as its operations and their
complexity. You will also learn about the definition of Records, its creation, declaration, and the
use of records in computer programming.
Page 1 of 18
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 structure is an aggregate entity containing one or more elements.
Page 2 of 18
CSC 204: Fundamentals of Data Structures
indirect addressing, and self-modifying code. Some early computers, such as the IBM 1620, had
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 3 of 18
CSC 204: Fundamentals of Data Structures
Decide on names for the overall structure and for the individual fields
Determine the data types of the fields
Example 1
type
record_type_name = record
field_name_1: field_type_1;
field_name_2: field_type_2;
Page 4 of 18
CSC 204: Fundamentals of Data Structures
end record;
Type
biodata=Record
name => “Mrs A.A Akpors”;
phone => “08022334455”;
sex => female;
age => 21;
weight =>62.0;
End;
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 5 of 18
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
Page 6 of 18
CSC 204: Fundamentals of Data Structures
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);
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);
Page 7 of 18
CSC 204: Fundamentals of Data Structures
Page 8 of 18
CSC 204: Fundamentals of Data Structures
Assuming that the word is the smallest individually transferable unit of store, it is evidently highly
desirable that s be a whole number, the simplest case being s = 1. If s is not a whole number (and
this is the normal case), then s is usually rounded up to the next larger integer S.
Each array component then occupies S-words, whereby S-s words are left unused (see Figs. 4.1 and
4.2). Rounding up of the number of words needed to the next whole number is called padding. The
storage utilization factor u is the quotient of the minimal amounts of storage needed to represent a
structure and of the amount actually used:
u = s / (s rounded up to nearest integer)
Page 9 of 18
CSC 204: Fundamentals of Data Structures
ki = S1 + S2 + ... + Si-1 k0 = 0
where sj is the size (in words) of the j-th component. 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 Fig. 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 10 of 18
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
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 (minus 1).
Page 11 of 18
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 12 of 18
CSC 204: Fundamentals of Data Structures
Page 13 of 18
CSC 204: Fundamentals of Data Structures
Pilot Answers
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, while a record is an heterogeneous collection of components.
Page 14 of 18
CSC 204: Fundamentals of Data Structures
Page 15 of 18
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 structure is 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 16 of 18
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 17 of 18
CSC 204: Fundamentals of Data Structures
i. 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.
ii. type
record_type_name = record
field_name_1: field_type_1;
field_name_2: field_type_2;
-- Various fields in the record
end record;
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. While Array is a homogeneous collection of components; all components of
the same kind, while a record is an heterogeneous collection of components.
iv. 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.
The array should be mapped in such a way that the computation of addresses of array
components is as simple (and therefore as efficient) as possible.
Page 18 of 18