KEMBAR78
Unit 16 Data Representation | PDF | Data Type | Pointer (Computer Programming)
0% found this document useful (0 votes)
25 views25 pages

Unit 16 Data Representation

The document discusses data representation in computer science, focusing on user-defined data types, including enumerated and composite types, as well as pointers. It explains the importance of user-defined data types for error reduction and data storage, and outlines file organization and access methods. Additionally, it covers floating-point representation, normalization, and the significance of storing numbers in normalized form for maximizing range and precision.

Uploaded by

syedebadahmed95
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)
25 views25 pages

Unit 16 Data Representation

The document discusses data representation in computer science, focusing on user-defined data types, including enumerated and composite types, as well as pointers. It explains the importance of user-defined data types for error reduction and data storage, and outlines file organization and access methods. Additionally, it covers floating-point representation, normalization, and the significance of storing numbers in normalized form for maximizing range and precision.

Uploaded by

syedebadahmed95
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/ 25

Unit 16

Data Representation
Who is the User?
We will get to hear the term “User” very often in the realm of Computer Science. It’s only fair shed some light on this term:-
- programmers write programs and users “use” them.
- the same ‘user’ is provided with an ‘user’ interface by the Operating System.
- finally, the a ‘user’ of a programming language is actually referring to the programmer.
- we are referring to this type of ‘user’ in User Defined Data Types.
Enumerated Data Types:-
Non-Composite User-Defined Data Types:- It is actually a list of possible values
A Non-Composite User Defined Data type is one that does TYPE
not have a reference to another data type. TDays =
These include Simple Built-In Data Types i.e. Integer, Real, (Monday,Tuesday,Wednesday,Thursday,Friday,Saturda
String, Etc. y,Sunday)
These defined simply by their identifiers and data types. TDirections = (North,East,West,South)
<identifier>:<type>. END TYPE
For example. MyNum : INTEGER Variable Declarations will look like:-
DECLARE <identifier> : <type>
DECLARE Direction1 : Tdirections
DECLARE StartDay : Tdays
Direction1ßNorth
StartDay ß Monday
Enumerated Data Types may look like String types but they are not as they
are not enclosed in ‘QUOTES’ and they should not be either. They values
in this type are Ordinal (each value has an order) which means the
satetment StartDay>Friday will return TRUE if StartDay has
Saturday or Sunday stored in it as they have a higher ordinal value than
Friday.
Pointers:- MyIntegerPointer ß@Number1
Pointers are used for storing addresses to memory locations. Pointer Assigning the address of an Integer pointer to a Pointer
definitions must include definition of the type of variable it is that can hold addresses of only integer types
pointing to. Number2ßMyIntegerPointer^ * 2
TYPE
The value which is stored in the address pointed by
TIntPointer = ^INTEGER
//pointer variable is multiplied by 2 and assigned to
A pointer type for only for pointer for integer variables Number2. The use of caret(^) represents
DECLARE MyIntegerPointer : TIntPointer “Dereferencing”.
Declaring a pointer variable MyIntegerPointer^ ß 200
DECLARE Number1,Number2 : INTEGER Declaration of
200 is assigned to the address pointed to by
two ordinary int variables MyInetgerPointer.
Number1ß100
Assigning a value to a Integer variables.
Composite User Defined Types:-
A user defined type that has a definition which includes a reference to at least one other data type. They include the following:-
• Record Data Type
• Set Data Type Set Data Type:-
• Objects (to be discussed later) A set data type allows a program to create sets and to apply the
mathematical operations defined in set theory. The following is a
Record Data Types:- representative list of the operations to be expected:
Contains a variety of components which can be of different data types. • union
For example:- • difference
TYPE • intersection
TEmployeeRecord • include /exclude elements from set
DECLARE EmployeeFirstName : STRING • check whether an element is in a set.
DECLARE EmployeeFamilyName : STRING TYPE <Set Identifier> : SET OF <Base Type>
DECLARE DateEmployed : DATE For example
DECLARE Salary : CURRENCY TYPE Tvowel : SET OF CHAR
ENDTYPE DEFINE vowel(‘a’, ‘e’, ‘i’, ‘o’, ‘u’):Tvowels
Declaration of a variable of type TEmployeeRecord will look like DECLARE firstVowel ß ‘a’
DECLARE Employee1:TEmployeeRecord DECLARE secondVowel ß ‘e’
And accessing individual items in a Record Data will look like:-
Employee1.DateEmployed ß #16/05/2017#
A possible application of record data type is when one or two of the
components of the record are pointers (used in Abstract Data Types
to be discussed Later)
Why are User Defined Data Types Needed?
For a reasonably large programs written using a OOP (Object Oriented Programming) Language User Defined Data types can:-
- Make the program less error prone and easier to understand.
- Allow for data storage if otherwise impossible with built in data types.
- If you are using only integer values only then the use of user defined data types may not be justified because integers are the same for all
programming languages.

File Organisation
All kinds of data can be stored in your computer. These “files” are stored with certain binary codes that allows
the file to be used in the way intended. Files can be of the following types:-
• Text Files:-
Contains data stored according to ASCII values (Unit 1) and that can be accessed using text editors. (Unit
13)
• Binary Files:-
A file designed to store data to be used by a computer program.
• for example an integer can be stored using 2 bytes in 2’s complement formation.
• Binary files are organised in the form of records (with different data in different fields)
File Access:-
Data can be stored in the following ways:-
Serial Files:- Direct Access:-
Data is stored with no defined order (other than being in It is also known as Random Access.
chronological order). For example a file containing a list Records are accessed immediately instead of traversing all records till desired
of transactions carried for a bank account. record is found. (ideal for large files)
Best for Creating Data Back-Ups For this to work the position of the file must be calculated according to the
Sequential Files:- files data. Then the same calculation is used when searching for data.
Data is stored in an ordered fashion, preferably for long One such method is “Hashing” in which a Hashing Algorithm is used to
term storage. calculate the position of a record (both at the time of storage and
Resembles the working of a Database. retrieval).
it could be the master file for a customer account
which is periodically updated when compared to
the transactions that have taken place.
Sequential files are ordered according to a Key Field values
of which are unique, in sequence but not necessarily 64636 23467
consecutive.
Sequential access works on the principle reading the value
in the key field until the desired value is found.
To delete or update data a copy of entire file is made until
the record is reached that requires deleting or editing
if deleting is needed then copying from old files
continues from the next record ignoring the one
that was meant to be deleted.
If editing is needed then an edited version of record
is stored in copied file.
Binary Normalised Floating Point Representation
M represents the mantissa
Real Numbers (the one with fractional part):-
E Represents the Exponent
Real numbers can be expressed using the following methods
R is not stored in the representation but implies the value 2
in denary number system:-
• Simple ideal for small numbers
• Scientific (Exponential) best for large numbers.
• .253 * 102
• 2.53 * 101
• 25.3 * 100
• 253 * 10-1

-128 64 32 16 -8 4 2 1 . ½ ¼
4.75 0 1 0 0 1 1 0100 11
-4.75 1 0 1 1 0 0 1’s comp
1 0 1 1 0 1 2’s comp 1011 01
Floating point representation using 8 bits.
MSB for Sign bit. Next 5 bits for whole
number portion and remaining 2 bits for
fractional part.

4 bits for Mantissa


Mantissa expressed using 4 bits (a)
and (b). The decimal point is
represented (temporarily) as a gap.
(c) Shows the range of values that
can represented when 4 bits are
used for exponent.

Using the representation


mentioned above, the most
important non-zero values are show
here.
Precision: -
A format must be decided upon for representation of floating values i.e., the number of bits for mantissa vs exponent. A
clear distinction between the two parts of a representation should be made for a floating-point processor. If the bits for
mantissa increase it would lead to MORE PRECISION. However, this would also result in lesser bits available for exponent
resulting in LESSER RANGE OF VALUES.
Consider the Binary Conversion of the 5.25
The method for conversion of a positive value is as follows:
1. 5 will be converted to 0101 like before adding a
1. Convert the whole-number part using the method described
sign bit to the left since we will be working with
in Unit 1.
2’s comp
2. Add the 0 sign bit as MSB
2. .25 will be treated as follows.
3. Convert the fractional part using the method described
3. .25 *2. = .5 since resulting number is not greater
4. Combine the two, with the exponent expressed as zero.
than 1 therefore à 0
4. .5 * 2 = 1 this process ends here as we have
reached 1 à 1
the resulting number is 0101 . 01
However both of these answers are in
UNNORMALISED form.
Normalisation Is Performed in the Following Manner:-
• For positive number, the bits in the mantissa are shifted left until the most significant bits are 0 followed by 1.
• Each shift left will result in the exponent reducing by one.
• The same process of shifting is used for a negative number until the most significant bits are 1 followed by 0.
• In this case, no attention is paid to the fact that bits are falling off the most significant end of the mantissa

Since the number 0101 . 01 (5.25) is positive we will apply rule 1


• the decimal point needs to be shifted to the left places until it reaches between a 0 and 1
• i.e. 0.10101 (this is achieved by the shifting the decimal point 3 places to the left….3 is represented as
101 in binary therefore our normalized method of showing 5.25 will be 010101 011

• 010101(is the known as the mantissa part) 011(is known as the exponent)
the number of bits may be specified for both mantissa and exponent.

*** It is also possible that your exam may have a question in which we already have a binary mantissa and exponent but not in
NORMALISED form…in which we case we can follow the following steps to achieve normalisation.
For positive number, the bits in the mantissa are shifted left until the most significant bits are 0 followed by 1.
Each shift left will result in the exponent reducing by one.

The same process of shifting is used for a negative number until the most significant bits are 1 followed by 0.
In this case, no attention is paid to the fact that bits are falling off the most significant end of the mantissa
Approximation of Floating Point Numbers:-
The some numbers cannot be completely converted to their binary fractional part…that is why we have to round off or approximate the
floating point portion:-
5.3
.3 * 2 = .6 à 0
.6 * 2 = 1.2 à1
.2 * 2 = .4 à 0
.4 * 2 = .8 à 0
.8 * 2 = 1.6 à 1
.6 (here you will see the process repeating again!)
therefore there is no definite converted value of this binary number and it is rather rounded off by the computer.

For example consider the following question

This is happening because numbers like 0.3 cannot be accurately converted into binary due to which it is approximated
(represented by a value just less than .3 and it could also be just greater as well. ) and the result looks likes.
the whole number part is three represented by 011
the .5 is represented by .1
Therefore the number is 011.1
without the decimal space will be 0.111 (this will mean 2
shifts to the left)

For this we will simply take the 2’s


complement of the mantissa…

Which will result in…


The exponent is 4 therefore we have to shift the
decimal point 4 places to the right getting 1110.02
1110.0 à 1410

The largest magnitude means that the


number has to be large value while being
negative. The number represented here is -
128 * 10 127
Exponent is equal to 3 so that means that the mantissa has to be shifted 3 places to the right.

the resulting number will be 10 1 (the gap can be assumed as being place for decimal point.

-32 16 4 2 1 . ½ ¼
0 0 0 1 0 1 0

As it can be seen the whole number part is result in a number 2 and the fractional part is resulting .5 therefore the number is +2.5
For positive numbers the mantissa has 0 as MSB followed by 1.
Here we can see the mantissa is 00101000. The rule states we will have to shift the mantissa to left to achieve 0 MSB Followed
by 1. For every shift left the exponent is reduced by one. Therefore if we shift the mantissa to the left the exponent will
become 2. resulting in
01010000 00000010
0.0625 in Normalised Binary

1 . .5 .25 .125 .0625


0 . 0 0 0 1

0.0625 à 0.0001 un-normalised form

Mantissa becomes 0.1 and exponent


becomes -3
Exponent is 0101 when converted to denary = 5
Why are numbers stored in normalised form?
• To store the maximum range of numbers in the minimum number of bytes / bits
• Normalisation minimises the number of leading zeros/ones represented
• Maximises precision of the number for the given number of bits
• enables very large / small numbers to be stored with accuracy.
• Avoids the possibility of many numbers having multiple representations.
Why are numbers stored in normalised form?
• To store the maximum range of numbers in the minimum number of bytes / bits
• Normalisation minimises the number of leading zeros/ones represented
• Maximises precision of the number for the given number of bits
• enables very large / small numbers to be stored with accuracy.
• Avoids the possibility of many numbers having multiple representations.

You might also like