01 Data Representation
01 Data Representation
Choose and design an appropriate user-defined data type for a given problem
Convert binary floating-point real numbers into denary and vice versa
Definitions
The programming language defines the range of possible
Built-in data type values that can be assigned to and the operations that can be
applied to a variable
01 Data Representation 1
Composite data type A data type that is derived from other data types
Non-Composite data
A data type defined without referencing another data type
type
Serial file organization Records stored in the order they were added in
Sequential file Physically stores record and ordered according to their key
organization field value
Random file organization Stores records of data in a file in any available position
Each record in the file is read, one by one, until the desired
Sequential Access
record is found
Normalized two’s
First and second bits of mantissa must be different for a
complement binary
floating point number to be normalized
number
01 Data Representation 2
The programming language defines the range of possible values that can be
assigned to and the operations that can be applied to a variable
Example:
TYPE SchoolDay = (Monday, Tuesday, Wednesday, Thursday, Friday)
Note: Since this is a different data type than STRING, quotation marks are not
used to represent them
Example:
MyVar ← 57
IntPointer ← @MyVar
MyVar2 ← IntPointer^
IntPointer^ ← 100
01 Data Representation 3
@ symbol before a variable identifier gives the address of the variable
Sets
A data type that allows storing a finite number of different values that have no
order
Example:
TYPE Sletter = SET OF CHAR
DEFINE vowel = (’a’, ‘e’, ‘i’, ‘o’, ‘u’) : Sletter
Classes
01 Data Representation 4
Skill Check 1
a. SchoolDay to hold data about the days students are usually in school.
[1]
b. WeekEnd to hold data about the days that are not school days. [1]
3. Define, using pseudocode, the composite data type ClubMeet. This will
hold data about club members that includes:
Solution
3. TYPE ClubMeet
DECLARE FirstName : STRING
DECLARE LastName : STRING
DECLARE Schoolday : SchoolDay
DECLARE Weekend : WeekEnd
ENDTYPE
01 Data Representation 5
1.2 File organization and access
1.2.1 File Organization
Serial File Organization
Records stored in the order they were added in
Justification:
The location of any record in the file is found by using a hashing algorithm on
the key field of a record
Low hit rate (only one record will match your account number/username)
Justification:
01 Data Representation 6
No need to search records.
Every record is searched until a record is found, or whole file has been
searched and not found, or if key field of current record being checked is
greater than the key field of record being searched in sequential file
organization
Direct Access
Jumps to a specific record in the file without accessing other records
For 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 random access files, a hashing algorithm is used on the key field to
calculate the address of the file location where a given record is stored
The result of the calculation gives the address where the record should be
found
To write a record:
01 Data Representation 7
To read a record:
If it does not match, then the following records need to be read until a
match is found (if open hash is used), or the overflow area needs to be
searched for a match (if closed hash is used)
Skill Check 2
3. State the most suitable method of file access when a bank stores its
data records in ascending order of account number. [1]
Solution
1. In both serial and sequential files, records are stored one after the
other and need to be accessed one after the other. Serial files are
stored in chronological order, whereas sequential files are stored
with ordered records and stored in the order of the key field. In serial
files, new records are added in the next available space. In
sequential files, new records are inserted in the correct position.
2. Direct Access
3. Sequential Access
01 Data Representation 8
M is the mantissa and E is the exponent
Add up all the mantissa values where a 1 bit appears to get the value of M
For the exponent consider the normal 8-bit binary number with the right most
bit representing 1
Add up the exponent values where a 1 bit appears to get the value of E
For example:
0.1011010 00000100
1 1 1 1 45
0.1011010= 2
+ 8
+ 16
+ 64
= 64
Exponent = 4
Represent the exponent by the number of places the decimal place is shifted
left to
01 Data Representation 9
For example:
4.5 →
0100.1000→ 0.1001000= Mantissa
00000011= Exponent
Normalization
First and second bits of mantissa must be different for a floating point number
to be normalized
Reasons:
To maximize the precision of the number for the given number of bits
To normalize, simply make the mantissa start with 1.0 (for a negative number)
or 0.1 (for a negative number) by shifting bits left
For example:
0.0011100 00000101
Shift bits left to get 0.1110000
Since it was shifted left, the value was increased for the relevant bit,
hence reduce the exponent by the number of left shifts, which is 2 in
01 Data Representation 10
this case
For a fixed number of bits, increasing one results in the decrease of other
and hence the trade-off
For a binary number with an 8-bit mantissa and an 8-bit exponent (using two’s
complement):
127
0111111101111111= 128
× 2127
The smallest positive number which can be stored is:
1
0100000010000000= 2
× 2−128
The smallest magnitude negative number which can be stored is:
65
1011111110000000= 128
× 2−128
The largest magnitude negative number which can be stored is:
1000000001111111= −1 × 2127
01 Data Representation 11
Underflow: Number is too small to be represented in the given format
01 Data Representation 12
Skill Check 3
1011100.011001
2. Explain the reason why binary numbers are stored in normalized form.
[3]
Solution
1.
a. Mantissa: 010111000110
Exponent:
0111
b. The accuracy of the number would be reduced because the least
significant bits of the original number have been lost.
Points To Note
01 Data Representation 13
Examples of non-composite user-defined data types include enumerated and
pointer data types
Record, set and class are examples of composite user-defined data types
01 Data Representation 14