13 Data Representation
13 Data Representation
13 Data representation
304
The values are not strings so are not enclosed in quotation marks
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.
305
13 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:
monthPointer ← ^thisMonth
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:
DECLARE myMonth : Tmonth
myMonth ← monthPointer^ monthPointer has been dereferenced
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.
306
The variable definition for a set includes the elements of the set.
DEFINE <identifier> (value1, value2, value3, ... ) :
<set-identifier>
Classes
A class is a composite data type that includes variables of given data types and
methods (code routines that can be run by an object in that class). An object is
defined from a given class; several objects can be defined from the same class.
Classes and objects will be considered in more depth in Chapter 20.
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.
307
2 Write pseudocode to carry out the following d) Append a line of text at the end of the file.
operations on a text file.
3 Write a program to test your pseudocode.
Key terms
Serial file organisation – a method of file organisation in of the record; the result of the calculation gives the
which records of data are physically stored in a file, one address where the record should be found.
after another, in the order they were added to the file. File access – the method used to physically find a
Sequential file organisation – a method of file record in the file.
organisation in which records of data are physically Sequential access – a method of file access in which
stored in a file, one after another, in a given order. records are searched one after another from the
Random file organisation – a method of file physical start of the file until the required record is
organisation in which records of data are physically found.
stored in a file in any available position; the location Direct access – a method of file access in which
of any record in the file is found by using a hashing a record can be physically found in a file without
algorithm on the key field of a record. physically reading other records.
Hashing algorithm (file access) – a mathematical
formula used to perform a calculation on the key field
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.
308
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:
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
309
13
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.
13
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:
When reading a record from a file using direct access, the address of the location
ACTIVITY 13E 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
1 Explain, using checked against the original key field to ensure that they match. If the key fields
examples,
do not match, then the following records need to be read until a match is found
the difference
between serial (open hash) or the overflow area needs to be searched for a match (closed hash).
and sequential
files.
2 Explain the
ACTIVITY 13D
process of direct
access to a A file of records is stored at address 500. Each record takes up five locations
record in a file and there is space for 1000 records. The key field for each record can take
using a hashing the value 1 to 9999.
algorithm. The hashing algorithm used to calculate the address of each record is the
3 Choose an remainder when the value of key field is divided by 1000 together with the
appropriate start address of the file and the size of the space allocated to each record.
file type for
the following Calculate the address to store the record with key field 9354.
situations. If this location has already been used to store a record and an open hash is
Give the reason used, what is the address of the next location to be checked?
for your choice in
each case.
a) Borrowing Hashing algorithms can also be used to calculate addresses from names. For
books from a example, adding up the ASCII values for every character in a name and dividing
library. this by the number of locations in the file could be used as the basis for a
b) Providing an hashing algorithm.
annual tax
statement for
employees at EXTENSION ACTIVITY 13B
the end of the
year. Write a program to
n find the ASCII value for each character in a name of up to 10 characters
c) Recording
daily rainfall n add the values together
readings at n divide by 1000 and find the remainder
a remote n multiply this value by 20 and add it to 2000
weather
n display the result.
station to
be collected If this program simulates a hashing algorithm for a file, what is the start
every month. address of the file and the size of each record?
311
into binary.
6 a) Standard form is sometimes
a) +48
used to put denary improper
b) +122 fractions into proper
c) −100 14
fractions. For example,
5
d) −55 1.4
can be written as × 101,
e) −2 112
5
and can be written as
2 Convert these binary numbers 3
into denary. 1.12
× 10 2.
3
a) 00110011
b) 01111110 Change the following
improper fractions into
c) 10110011
proper fractions using this
d) 11110010 format:
e) 11111111 21
i)
3 Use two’s complement to find 5
the negative values of these 117
binary numbers. ii)
4
a) 00110100 558
iii)
b) 00011101 20
c) 01001100 b) When using binary, we
d) 00111111 can convert improper
e) 01111110 binary fractions into
4 Carry out these binary additions, proper fractions. For
7
showing all your working. example, 2 can be written
a) 00110001 + 00011110 77
as ××44(where 4 ≡422≡) 22 ),
(where
8
8 23
b) 01000001 + 00111111
and can be written as
c) 00111100 + 01000101 2
23
23
) 2 ).
4
×16(where16
× 16(where16≡ 24≡
d) 01111101 + 01011100 32
32
e) 11101100 + 01100000 Change the following
f) 10001111 + 10011111 improper binary fractions into
g) 01000101 + 10111100 proper binary fractions using
h) 01111110 + 01111110 this format.
i) 11111100 + 11100011 i)
11
2
j) 11001100 + 00011111
5 Write the following numbers in 41
ii)
standard form 4
312
1 1 1 1 1
10 100 1000 10 000 100 000
10 1
• 3 1 2 1 1 × 2 4
mantissa values exponent
▲ Figure 13.6
We thus get the binary floating-point equivalent (using 8 bits for the mantissa
1
and 8 bits for the exponent with the assumed binary point between –1 and in
2
the mantissa):
1 1 1 1 1 1 1
−1 • 2 4 16
−128 64 32 16 8 4 2 1
8 32 64 128
▲ Figure 13.7
313
1 1 1 1 1 1 1
−1 • 2 4 8 16 32 64 128
−128 64 32 16 8 4 2 1
0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 0
13 Data representation
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
1 1 1 1 32 8 4 1 45
M= + + + ≡ + + + =
2 8 16 64 64 64 64 64 64
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.
0 1 0 1 1 0 1 0
This gives 11.25 (the same result as method 1).
1 1 1 1 1 1 1
−1 • 2 4 8 16 32 64 128
−128 64 32 16 8 4 2 1
0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1
314
0 0 1 0 1 0 0 0
1 1 1 1 1 1 1
−1 • 2 4 8 16 32 64 128
−128 64 32 16 8 4 2 1
1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
1 1 1 16 2 1 19 32 19 13
M = −1 + + + ≡ −1 + + + ≡ −1 + ≡− + =−
2 16 32 32 32 32 32 32 32 32
315
13 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.
13 Data representation
fraction
whole number part
part
1
−4096 2048 1024 512 256 128 64 32 6 8 4 2 1 • 2
0 0 1 1 0 1 0 0 0 0 0 0 0 0
This gives −(1024 + 512 + 128) = −1664 (the same result as method 1).
1 1 1 1 1 1 1
−1 • 2 4 8 16 32 64 128
−128 64 32 16 8 4 2 1
1 1 0 0 1 1 0 0 1 1 1 1 1 1 0 0
Solution
Method 1
Add up the mantissa values where a 1 bit appears:
1 1 1 16 2 1 19 32 19 13
M = −1 + + + ≡ −1 + + + ≡ −1 + ≡− + =−
2 16 32 32 32 32 32 32 32 32
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.
316
0 0 0 0 0 0 1 1 0 1 0 0
This gives − ( 1
+
1
+
1
)=− 13
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) 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1
b) 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1
c) 0 1 1 1 0 0 0 0 1 1 1 1 1 0 1 1
d) 0 0 0 1 1 1 1 0 1 1 1 1 1 1 0 0
e) 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1
f) 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0
g) 1 1 1 1 0 1 0 0 0 0 0 0 0 1 0 0
h) 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1
i) 1 0 1 1 0 0 0 0 1 1 1 1 1 1 0 1
j) 1 1 1 0 0 0 0 0 1 1 1 1 1 0 1 0
Solution
Method 1
Turn the number into an improper fraction:
9
4.5 =
2
The fraction needs to be < 1, which means the numerator < denominator; we can
do this by dividing successively by 2 until the denominator > numerator.
9 9 9 9
→ → → The numerator (9) is now < denominator (16).
2 4 8 16
317
And the exponent is 23, which is represented as 11 in our binary f loating point
format.
13 Data representation
0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1
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:
0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1
Solution
Method 1
Remember, the fraction needs to be < 1, which means the numerator <
denominator.
171875 11
0.171875 ≡ ≡ , so this fraction is already in the correct form.
1000 000 64
11 1 1 1
= + + , which gives the mantissa as 0.0010110 and exponent as 0.
64 8 32 64
0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0
Method 2
0 = 0 and .171875 = .001011 ( 1
+
8 32
1
+
1
64 ), 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.
318
0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 0
0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0
0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 0
Solution
Method 1
Turn the number into an improper fraction:
3 83
0.375 ≡ , so − 10.375 = −
8 8
Now make the fraction < 1.
83 83 83
− ≡− × 16 ≡− × 24
8 128 128
83 45
− = −1 + , which gives the mantissa as 1.0101101
128 128
( 45
128
= +
1
4
1
+
16 32
1
+
1
128 ).
And the exponent is 24, which is represented as 100 in our binary f loating point
format.
Filling in the gaps with 0s gives:
1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0
Method 2
1 1
−10 = −01010 and + ≡.375 = .011, which gives: −01010.011.
4 8
Using two’s complement (on 01010011) we get: 10101100 + 1 = 10101101
(= 10101.101).
Now move the binary point as far as possible until 1.0 can be formed:
319
1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0
ACTIVITY 13G
1 Write these into binary floating-point format using an 8-bit mantissa and
8-bit exponent.
11 11
a) × 27 f) − × 24
32 64
19 9
b) × 23 g) − × 2−1
64 16
21 5
c) × 2−5 h) − × 25
128 16
15 1
d) × 2−3 i) − × 2−6
16 4
21 5
e) × 23 j) − × 2−2
8 8
2 Convert these denary numbers into binary floating-point numbers using
an 8-bit mantissa and 8-bit exponent.
15
a) +3.5 f) −
32
b) 0.3125 g) −3.5
c) 15.375 h) −10.25
41 3
d) i) −1.046875 ≡ −1
64 64
11
e) 9.125 j) −3
32
320
13
mantissa and 8-bit exponent.
To convert this into binary, we will use a method similar to that used in Chapter 1.
.88 × 2 = 1.76 so we will use the 1 value to give 0.1
.76 × 2 = 1.52 so we will use the 1 value to give 0.11
.52 × 2 = 1.04 so we will use the 1 value to give 0.111
.04 × 2 = 0.08 so we will use the 0 value to give 0.1110
1
0.1000000 00000010 ≡ × 22 =2
2
1
0.0100000 00000011 ≡ × 23 =2
4
1
0.0010000 00000100 ≡ × 24 =2
8
1 =2
0.0001000 00000101 ≡ × 25
16
▲ Figure 13.8
321
13
sequence, if we kept shifting to the right, we would end up with:
0.0000000 00001001 =2
▲ Figure 13.9
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
Normalise 0.0011100 00000101 ≡ ( 7
32
× 25 )
=7 .
Solution
Shift the bits left to get 0.1110000.
Since the bits were shifted two places left, the exponent must reduce by two to
give 00000011.
This gives 0.1110000 00000011, which is now normalised.
7
Note: 0.1110000 00000011 ≡ × 2 3 = 7, so the normalised form still represents
8
the correct value.
Example 13.9
Normalise 1.1101100 00001010 ≡ − ( 5
32
× 210 = −160 )
Solution
Shift the bits left until to get 1.0110000.
Since the bits were shifted two places left, the exponent must reduce by two to
give 00001000.
This gives 1.011000 00001000, which is now normalised.
Note: 1.011000 00001000 ≡ − 5 × 28 = −5 × 32 = −160 , so the normalised form
8
still represents the same value.
322
127
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 ≡ × 2127
128
▲ Figure 13.10
1
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ≡ × 2−128
2
▲ Figure 13.11
65
1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 ≡− × 2−128
128
▲ Figure 13.12
1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 ≡ −1 × 2127
▲ Figure 13.13
13 1 0
▲ Figure 13.14
1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
2 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
▲ Figure 13.15
3 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
▲ Figure 13.16
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.
324
End of chapter 1 A computer holds binary f loating-point numbers in two’s complement form with
questions the binary point immediately after the left-most bit.
A 24-bit word is used as follows:
mantissa exponent
Ⓐ 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
Ⓑ 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Ⓒ 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
13
c) Discuss the problems of representing the number zero in normalised
f loating-point format. [2]
2 A computer uses 12 bits for the mantissa and 6 bits for the exponent.
a) Convert these binary f loating-point numbers into denary.
i) 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1 1 [3]
ii) 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0 [3]
13 Data representation
mantissa exponent
mantissa exponent
326
13
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
random
sequential
serial
direct
sequential
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.
Justify your choice. [9]
Cambridge International AS & A Level Computer Science 9608
Paper 32 Q4 June 2017
327