0% found this document useful (0 votes)
27 views16 pages

CSC 204 Session 4

Uploaded by

jfixcoding
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

CSC 204 Session 4

Uploaded by

jfixcoding
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CSC 204: Fundamentals of Data Structures

Study Session 4: Record Structures

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.

Learning Outcomes for Study Session 4


On completion of this study session, you should be able to:
4.1 Explain the overview of Record Structure and differences between arrays and records.
4.2 Describe the Designing, Declaring and the use of Records.
4.3 Briefly explain the principle of Array of Records.
4.4 Explain the representation of Arrays, Records and Sets.

Page 70 of 269
CSC 204: Fundamentals of Data Structures

4.1 Overview of Record Structures


In computer science, 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.

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.

Box 4.1: What is Record and Record Structure?

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.

4.1.1 History of Records


The concept of record can be traced to various types of tables and ledgers used in accounting since
remote times. The modern notion of records in computer science, with fields of well-defined type
and size, was already implicit in 19th century mechanical calculators, such as Babbage's Analytical
Engine.
Records were well established in the first half of the 20th century, when most data processing was
done using punched cards. Typically each record of a data file would be recorded in one punched
card, with specific columns assigned to specific fields.
Most machine language implementations and early assembly languages did not have special syntax
for records, but the concept was available (and extensively used) through the use of index registers,
indirect addressing, and self-modifying code. Some early computers, such as the IBM 1620, had

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.

4.1.2 Differences between Arrays and Records


Remember that an Array is a homogeneous collection of components; all components of the same
kind. A record is a heterogeneous collection of components. In other words, the components may
be of different kinds. A record is a compound data structure consisting of a number of components,
usually called fields. Think of a record as a template that outlines each of the record’s fields.

Pilot Question 4.1


i. Given
A= {Osun, Lagos, Kogi, Ondo, Kwara, Rivers}
B={x, y, z, %,*, #}
C= {Rectangle, Square, Circle, Parallelogram, Triangle, Trapezium}
D= {Name, Age, Sex, Phone_Number, Contact_Address, Television, Radio}
Identify from A,B,C,D arrays and records.
ii. Name programming languages that support Records

iii. Define Record

4.2 Designing, Declaring and the use of Records


4.2.1 Designing Records
To design, a record the following steps must be considered;
 Identify the items of data that are relevant in the application
 Use a data structure diagram to show the relevant information
 Decide on names for the overall structure and for the individual fields
 Determine the data types of the fields

Page 72 of 269
CSC 204: Fundamentals of Data Structures

Example 1

Name : {names}; --string sub-type

Phone : {phone number}; --string sub-type

Sex : {M/F}; --character type

Age : {ages}; --integer

Weight : {weights}; --real

4.2.2 Declaring records

Declaration of record data type format is as follows:

type

record_type_name =record

field_name_1: field_type_1;

field_name_2: field_type_2;

-- Various fields in the record

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

Figure 4.1: Example 2

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.

4.2.3 Using records


i. To refer to an entire record variable (for assignment, parameter, comparison, etc), just use
its name
For Example; persons
ii. To refer to a field of a record, use record_name.field_name
For Example: persons.name
iii. Assignment
– You can assign one record variable to another of identical type
that_person:= this_person;
iv. Input
– You cannot read an entire record variable in a single operation. You must read each field
separately.
– To input a record variable use a procedure:

Page 74 of 269
CSC 204: Fundamentals of Data Structures

Prompt for and get each field in turn

Pilot Question 4.2

i. Write out the declaration of record data type format.

4.3 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.

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.

Pilot Question 4.3


i. Explain the Array of record
ii. Differentiate between an Array and a Record

4.4 Representation of Arrays, Records, and Sets


The essence of the use of abstractions in programming is that a program may be conceived,
understood and verified on the basis of the laws governing the abstractions, and that it is not
necessary to have further insight and knowledge about the ways in which the abstractions are
implemented and represented in a particular computer.

Page 76 of 269
CSC 204: Fundamentals of Data Structures

Nevertheless, it is essential for a professional programmer to have an understanding of widely used


techniques for representing the basic concepts of programming abstractions, such as the
fundamental data structures. It is helpful as it might enable the programmer to make sensible
decisions about program and data design in the light not only of the abstract properties of
structures, but also of their realizations on actual computers, taking into account a computer's
particular capabilities and limitations.
4.4.1 Representation of Arrays
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. The address i of the j-th array component is computed by the linear mapping
function:
i = i0 + j*s
Where i0 is the address of the first component, and s is the number of words that a component
occupies.
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 Figure 4.2
and 4.3) 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 77 of 269
CSC 204: Fundamentals of Data Structures

Figure 4.2: Mapping an array onto a store

Figure 4.3: Padded representation of a record

4.4.2 Representation of Records


Records are mapped onto a computer store by simply juxtaposing their components. The address of
a component (field) ri relative to the origin address of the record r is called the field's offset ki. It is
computed as:

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 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

Figure 4.4: Representation of a packed record

4.4.3 Representation of Sets


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”.
As an example, the set of small integers S = {2, 3, 4, 7, 11, 13} is represented by the sequence of
bits, by a bit string:
C(s) = (… 0010100010101100)

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

(x = c1) OR (x = c2) OR ... OR (x = cn)

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.

Pilot Question 4.4


i. Explain the representation of arrays

Page 80 of 269
CSC 204: Fundamentals of Data Structures

Summary of Study Session 4


In this Study Session 4, you have learnt that:

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.

Pilot Answer 4.2

type

record_type_name =record

field_name_1: field_type_1;

field_name_2: field_type_2;

-- Various fields in the record

end record;

Pilot Answer 4.3


i. 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

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.

Pilot Answer 4.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. The array should be

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.

Representation of an array structure: 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.

Page 84 of 269
CSC 204: Fundamentals of Data Structures

Self-Assessment Questions (SAQs) for Study Session 4


Now that you have completed this study session, you can assess how well you have achieved its
Learning Outcomes by answering the following questions.

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

You might also like