Data Representation
Data Representation
Programmers use specific data types that exactly match a program’s requirements. They define
their own data types based on primitive data types provided by a programming language, or data
types that they have defined previously in a program. These are called user-defined data types.
User-defined data types can be divided into non-composite and composite data types.
Key terms
User-defined data type – a data type based on an existing data type or other data types that
have been defined by a programmer.
Non-composite data type – a data type that does not reference any other data types.
Enumerated data type – a non-composite data type defined by a given list of all possible
values that has an implied order.
Pointer data type – a non-composite data type that uses the memory address of where the data
is stored.
Set – a given list of unordered elements that can use set theory operations such as intersection
and union.
13.1.1 Non-composite data types
A non-composite data type can be defined without referencing another data type. It can be a
primitive type available in a programming language or a user-defined data type. Non-composite
user-defined data types are usually used for a special purpose.
We will consider enumerated data types for lists of items and pointers to data in a computer’s
memory.
For example, a data type for months of the year could be defined as:
Then the variables thisMonth and nextMonth of type Tmonth could be defined as:
ACTIVITY 13A
Using pseudocode, declare an enumerated data type for the days of the week. Then declare two
variables today and yesterday, assign a value of Wednesday to today, and write a suitable
assignment statement for tomorrow.
Pointer data type
A pointer data type is used to reference a memory location. This data type needs to have
information about the type of data that will be stored in the memory location. In pseudocode the
type definition has the following structure, in which ^ shows that the type being declared is a
pointer and <Typename> is the type of data to be found in the memory location, for example
INTEGER or REAL, or any user-defined data type.
For example, a pointer for months of the year could be defined as follows:
If the contents of the memory location are required rather than the address of the memory
location, then the pointer can be dereferenced. For example, myMonth can be set to the value
stored at the address monthPointer is pointing to:
ACTIVITY 13B
Using pseudocode for the enumerated data type for days of the week, declare a suitable pointer
to use. Set your pointer to point at today. Remember, you will need to set up the pointer data
type and the pointer variable.
13.1.2 Composite data types
A data type that refers to any other data type in its type definition is a composite data type. In
Chapter 10, the data type for record was introduced as a composite data type because it refers to
other data types.
Sets
A set is a given list of unordered elements that can use set theory operations such as intersection
and union. A set data type includes the type of data in the set. In pseudocode, the type definition
has this structure:
The variable definition for a set includes the elements of the set.
ACTIVITY 13C
1 Explain, using examples, the difference between composite and non-composite data types.
2 Explain why programmers need to define user-defined data types.
Use examples to illustrate your answers.
3 Choose an appropriate data type for the following situations.
Give the reason for your choice in each case.
a) A fixed number of colours to choose from.
b) Data about each house that an estate agent has for sale.
c) The addresses of integer data held in main memory.
13.2 File organisation and access
WHAT YOU SHOULD ALREADY KNOW
Try these three questions before you read the second part of this chapter.
1 Describe three different modes that files can be opened in.
2 Write pseudocode to carry out the following operations on a text file.
a) Create a text file.
b) Write several lines of text to the file.
c) Read the text that you have written to the file.
d) Append a line of text at the end of the file.
3 Write a program to test your pseudocode.
Key terms
Serial file organisation – a method of file organisation in which records of data are physically
stored in a file, one after another, in the order they were added to the file.
Sequential file organisation – a method of file organisation in which records of data are
physically stored in a file, one after another, in a given order.
Random file organisation – a method of file organisation in which records of data are
physically stored in a file in any available position; the location of any record in the file is
found by using a hashing algorithm on the key field of a record.
Hashing algorithm (file access) – a mathematical formula used to perform a calculation on
the key field of the record; the result of the calculation gives the address where the record
should be found.
File access – the method used to physically find a record in the file.
Sequential access – a method of file access in which records are searched one after another
from the physical start of the file until the required record is found.
Direct access – a method of file access in which a record can be physically found in a file
without physically reading other records.
13.2.1 File organisation and file access
File organisation
Computers are used to access vast amounts of data and to present it as useful information.
Millions of people expect to be able to retrieve the information they need in a useful form when
they ask for it. This information is all stored as data in files, everything from bank statements to
movie collections. In order to be able to find data efficiently it needs to be organised. Data of all
types is stored as records in files. These files can be organised using different methods.
Serial file organisation
The serial file organisation method physically stores records of data in a file, one after another,
in the order they were added to the file.
Figure 13.1
New records are appended to the end of the file. Serial file organisation is often used for
temporary files storing transactions to be made to more permanent files. For example, storing
customer meter readings for gas or electricity before they are used to send the bills to all
customers. As each transaction is added to the file in the order of arrival, these records will be in
chronological order.
Sequential file organisation
The sequential file organisation method physically stores records of data in a file, one after
another, in a given order. The order is usually based on the key field of the records as this is a
unique identifier. For example, a file could be used by a supplier to store customer records for
gas or electricity in order to send regular bills to each customer. All records are stored in
ascending customer number order, where the customer number is the key field that uniquely
identifies each record.
Figure 13.2
New records must be added to the file in the correct place; for example, if Customer 5 is added to
the file, the structure becomes:
Figure 13.3
Random file organisation
The random file organisation method physically stores records of data in a file in any available
position. The location of any record in the file is found by using a hashing algorithm (see
Section 13.2.2) on the key field of a record.
Figure 13.4
File access
There are different methods of file access (the method used to physically find a record in the
file). We will consider two of them: sequential access and direct access.
Sequential access
The sequential access method searches for records one after another from the physical start of the
file until the required record is found, or a new record can be added to the file. This method is
used for serial and sequential files.
For a serial file, if a particular record is being searched for, every record needs to be checked
until that record is found or the whole file has been searched and that record has not been found.
Any new records are appended to the end of the file.
For a sequential file, if a particular record is being searched for, every record needs to be checked
until the record is found or the key field of the current record being checked is greater than the
key field of the record being searched for. The rest of the file does not need to be searched as the
records are sorted on ascending key field values. Any new records to be stored are inserted in the
correct place in the file. For example, if the record for Customer 6 was requested, each record
would be read from the file until Customer 7 was reached. Then it would be assumed that the
record for Customer 6 was not stored in the file.
Figure 13.5
Sequential access is efficient when every record in the file needs to be processed, for example, a
monthly billing or payroll system. These files have a high hit rate during the processing as nearly
every record is used when the program is run.
Direct access
The direct access method can physically find a record in a file without other records being
physically read. Both sequential and random files can use direct access. This allows specific
records to be found more quickly than using sequential access.
Direct access is required when an individual record from a file needs to be processed. For
example, when a single customer record needs to be updated when the customer’s phone number
is changed. Here, the file being processed has a low hit rate as only one of the records in the file
is used.
For a sequential file, an index of all the key fields is kept and used to look up the address of the
file location where a given record is stored. For large files, searching the index takes less time
than searching the whole file.
For a random access file, a hashing algorithm is used on the key field to calculate the address of
the file location where a given record is stored.
13.2.2 Hashing algorithms
In the context of storing and accessing data in a file, a hashing algorithm is a mathematical
formula used to perform a calculation on the key field of the record. The result of the calculation
gives the address where the record should be found. More complex hashing algorithms are used
in the encryption of data.
Here is an example of a simple hashing algorithm:
If a file has space for 2000 records and the key field can take any values between 1 and 9999,
then the hashing algorithm could use the remainder when the value of key field is divided by
2000, together with the start address of the file and the size of the space allocated to each record.
In the simplest case, where the start address is 0 and each record is stored in one location.
To store a record identified by a key field with value 3024, the hashing algorithm would give
address 1024 as the location to store the record.
Unfortunately, storing another record with a key field 5024 would result in trying to use the same
file location and a collision would occur.
This often happens with hashing algorithms for direct access to records in a file.
There are two ways of dealing with this:
1 An open hash where the record is stored in the next free space.
2 A closed hash where an overflow area is set up and the record is stored in the next free space
in the overflow area.
When reading a record from a file using direct access, the address of the location to read from is
calculated using the hashing algorithm and the key field of the record stored there is read. But,
before using that record, the key field must be checked against the original key field to ensure
that they match. If the key fields do not match, then the following records need to be read until a
match is found (open hash) or the overflow area needs to be searched for a match (closed hash).
ACTIVITY 13D
A file of records is stored at address 500. Each record takes up five locations and there is space
for 1000 records. The key field for each record can take the value 1 to 9999.
The hashing algorithm used to calculate the address of each record is the remainder when the
value of key field is divided by 1000 together with the start address of the file and the size of
the space allocated to each record.
Calculate the address to store the record with key field 9354.
If this location has already been used to store a record and an open hash is used, what is the
address of the next location to be checked?
Hashing algorithms can also be used to calculate addresses from names. For example, adding up
the ASCII values for every character in a name and dividing this by the number of locations in
the file could be used as the basis for a hashing algorithm.
ACTIVITY 13E
1 Explain, using examples, the difference between serial and sequential files.
2 Explain the process of direct access to a record in a file using a hashing algorithm.
3 Choose an appropriate file type for the following situations.
Give the reason for your choice in each case.
a) Borrowing books from a library.
b) Providing an annual tax statement for employees at the end of the year.
c) Recording daily rainfall readings at a remote weather station to be collected every month.
13.3 Floating-point numbers, representation
and manipulation
WHAT YOU SHOULD ALREADY KNOW
Try these six questions before you read the third part of this chapter.
1 Convert these denary numbers into binary.
a) +48
b) +122
c) −100
d) −55
e) −2
2 Convert these binary numbers into denary.
a) 00110011
b) 01111110
c) 10110011
d) 11110010
e) 11111111
3 Use two’s complement to find the negative values of these binary numbers.
a) 00110100
b) 00011101
c) 01001100
d) 00111111
e) 01111110
4 Carry out these binary additions, showing all your working.
a) 00110001 + 00011110
b) 01000001 + 00111111
c) 00111100 + 01000101
d) 01111101 + 01011100
e) 11101100 + 01100000
f) 10001111 + 10011111
g) 01000101 + 10111100
h) 01111110 + 01111110
i) 11111100 + 11100011
j) 11001100 + 00011111
5 Write the following numbers in standard form
a) 123 000 000
b) 2 505 000 000 000 000
c) –1200
d) 0.000000002341
e) −0.0000124005
6 a) Standard form is sometimes used to put denary improper fractions into proper fractions.
For example, can be written as , and can be written as .
Change the following improper fractions into proper fractions using this format:
i)
ii)
iii)
b) When using binary, we can convert improper binary fractions into proper fractions. For
example, can be written as (where 4 ≡ 22), and can be written as
(where 16 ≡ 24).
Change the following improper binary fractions into proper binary fractions using this
format.
i)
ii)
iii)
Key terms
Mantissa – the fractional part of a floating point number.
Exponent – the power of 2 that the mantissa (fractional part) is raised to in a floating-point
number.
Binary floating-point number – a binary number written in the form M × 2E (where M is the
mantissa and E is the exponent).
Normalisation (floating-point) – a method to improve the precision of binary floating-point
numbers; positive numbers should be in the format 0.1 and negative numbers in the format 1.0.
Overflow – the result of carrying out a calculation which produces a value too large for the
computer’s allocated word size.
Underflow – the result of carrying out a calculation which produces a value too small for the
computer’s allocated word size.
13.3.1 Floating-point number representation
In Chapter 1, we learnt about how binary numbers can be stored in a fixed-point representation.
The magnitude of the numbers stored depends on the number of bits used. For example, 8 bits
allowed a range of −128 to +127 (using two’s complement representation) whereas 16 bits
increased this range to −16 384 to +16 383.
However, this type of representation limits the range of numbers and does not allow for
fractional values. To increase the range, and to allow for fractions, we can look to the method
used in the denary number system.
For example, 312 110 000 000 000 000 000 000 can be written as 3.1211 × 1023 using scientific
notation. If we adopt this system in binary, we get:
M × 2E
M is the mantissa and E is the exponent.
This is known as binary floating-point representation.
In our examples, we will assume a computer is using 8 bits to represent the mantissa and 8 bits to
store the exponent (a binary point is assumed to exist between the first and second bits of the
mantissa). Again, using denary as our example, a number such as 0.31211 × 1024 means:
Figure 13.6
We thus get the binary floating-point equivalent (using 8 bits for the mantissa and 8 bits for the
exponent with the assumed binary point between –1 and in the mantissa):
Figure 13.7
Example 13.1
Convert this binary floating-point number into denary.
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
Method 2
Write the mantissa as 0.1011010.
The exponent is 4, so move the binary point four places to the right (to match the exponent
value). This gives 01011.010.
Example 13.2
Convert this binary floating-point number into denary.
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
Method 2
Write the mantissa as 0.0101000.
The exponent is 3, so move the binary point three places to the right (to match the exponent
value). This gives 0010.1000.
Example 13.3
Convert this binary floating-point number into denary.
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
Add up the exponent values where a 1 bit appears:
Method 2
Since the mantissa is negative, first convert the value using two’s complement.
So, write the mantissa as 00110011 + 1 = 00110100.
This gives −0.0110100.
The exponent is 12, so move the binary point 12 places to the right (to match the exponent
value). This gives −0011010000000.0.
This gives −(1024 + 512 + 128) = −1664 (the same result as method 1).
Example 13.4
Convert this binary floating-point number into denary.
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
Add up the exponent values where a 1 bit appears:
Method 2
Since the mantissa is negative, first convert the value using two’s complement.
So, write the mantissa as 00110011 + 1 = 00110100.
This gives −0.0110100.
The exponent is −4, so move the binary point four places to the left (to match the negative
exponent value). This gives −0.00000110100.
ACTIVITY 13F
Convert these binary floating-point numbers into denary numbers (the mantissa is 8 bits and
the exponent is 8 bits in all cases).
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
The fraction needs to be < 1, which means the numerator < denominator; we can do this by
dividing successively by 2 until the denominator > numerator.
So, can be written as and the original fraction is now written in the correct
format, M × 2E.
Method 2
4 = 0100 and .5 = .1 which gives: 0100.1
Now move the binary point as far as possible until 0.1 can be formed:
0100.1 becomes 0.1001 by moving the binary point three places left.
So, the exponent must increase by three:
0.1001 × 11
Filling in the gaps with 0s gives:
Example 13.6
Convert +0.171875 into a binary floating-point number.
Solution
Method 1
Remember, the fraction needs to be < 1, which means the numerator < denominator.
, so this fraction is already in the correct form.
, which
gives the mantissa as 0.0010110 and exponent as 0.
Filling in the gaps with 0s gives:
Method 2
0 = 0 and .171875 = .001011
, which gives:
0.001011.
Now move the binary point as far as possible until 0.1 can be formed:
0.001011 becomes 0.1011 by moving the binary point two places right.
So, the exponent must increase by two (in other words, −2).
The number 2, using eight bits is 00000010.
Applying two’s complement gives us 11111101 + 1 = 11111110
Thus, we have:
0.1011 × 11111110
Filling in the gaps with 0s gives:
Example 13.7
Convert −10.375 into a binary floating-point number.
Solution
Method 1
Turn the number into an improper fraction:
And the exponent is 24, which is represented as 100 in our binary floating point format.
Filling in the gaps with 0s gives:
Method 2
ACTIVITY 13G
1 Write these into binary floating-point format using an 8-bit mantissa and 8-bit exponent.
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
2 Convert these denary numbers into binary floating-point numbers using an 8-bit mantissa
and 8-bit exponent.
a) +3.5
b) 0.3125
c) 15.375
d)
e) 9.125
f)
g) −3.5
h) −10.25
i)
j)
number 2 (such as ). We will now consider numbers which can only be represented as an
approximate value (the accuracy of which will depend on the number of bits that make up the
mantissa).
The representation of the following example (denary number 5.88), using an 8-bit mantissa and
8-bit exponent, will lead to an inevitable rounding error since it is impossible to convert the
denary number into an exact binary equivalent.
This error could be reduced by increasing the size of the mantissa; for example, a 16-bit mantissa
would allow the number 5.88 to be represented as 5.875, which is a better approximation.
We will consider how to represent the denary number 5.88 using an 8-bit mantissa and 8-bit
exponent.
To convert this into binary, we will use a method similar to that used in Chapter 1.
We have to stop here since our system uses a maximum of 8 bits. Now the value of 5 (in binary)
is 0101; this gives:
5.88 ≡ 0101.11100001
Moving the binary point as far to the left as possible gives:
0.1011100 × 23 (23 since the binary point was moved three places).
Figure 13.8
As shown, there are several ways of representing the number 2. Using this sequence, if we kept
shifting to the right, we would end up with:
Figure 13.9
This could lead to problems. To overcome this, we use a method called normalisation.
With this method, for a positive number, the mantissa must start with 0.1 (as in our first
representation of 2 above). The bits in the mantissa are shifted to the left until we arrive at 0.1;
for each shift left, the exponent is reduced by 1. Look at the examples above to see how this
works (starting with 0.0001000 we shift the bits 3 places to the left to get to 0.100000 and we
reduce the exponent by 3 to now give 00000010, so we end up with the first representation!).
For a negative number the mantissa must start with 1.0. The bits in the mantissa are shifted until
we arrive at 1.0; again, the exponent must be changed to reflect the number of shifts.
Example 13.8
Example 13.9
ACTIVITY 13H
Normalise these binary floating-point numbers.
a) 0. 0 0 0 1 1 0 1 0 0 0 0 0 1 1 0
b) 0. 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1
c) 0. 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0
d) 0. 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1
e) 0. 0 0 1 1 1 0 0 0 0 0 0 1 0 0 0
f) 1. 1 1 1 1 0 0 0 0 0 0 0 1 0 0 0
g) 1. 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0
h) 1. 1 1 1 0 1 1 0 0 0 0 0 0 0 1 1
i) 0. 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0
j) 1. 1 1 1 1 0 0 0 1 1 1 1 0 1 0 0
Figure 13.10
Figure 13.11
Figure 13.12
Figure 13.13
• The accuracy of a number can be increased by increasing the number of bits used in the
mantissa.
• The range of numbers can be increased by increasing the number of bits used in the exponent.
• Accuracy and range will always be a trade-off between mantissa and exponent size.
Consider the following three cases.
Figure 13.14
This gives a largest positive value of ; which gives high accuracy but small range.
Figure 13.15
This gives a largest positive value of , which gives reduced accuracy but increased
range.
Figure 13.16
This gives a largest possible value of , which gives poor accuracy but extremely high
range.
Floating-point problems
The storage of certain numbers is an approximation, due to limitations in the size of the mantissa.
This problem can be minimised when using programming languages that allow for double
precision and quadruple precision.
a) When running this program the expected output would be 0.1, 0.2, 0.3, …, 5.0.
Explain why the output from this program gave values such as 0.399999 rather than 0.4.
b) Run the program on your own computer using a language such as Pascal.
Does it confirm the above statement?
c) Discuss ways of overcoming the error(s) described in your answer to part a).
b) Write pseudocode to declare a variable Journal of type TJournalRecord and assign the
following values to the variable.
[3]
Title – Spring Flowers
Author – H Williams
Publisher – XYZ Press
Number of pages – 40
Season – Spring
5 a) Three file organisation methods and two file access methods are shown below. Copy the
diagram below and connect each file organisation method to its appropriate file access
method(s).
[4]
For each of the files A, B and C, state an appropriate file organisation method for the use
given in the table.
All three file organisation methods must be different.