0% found this document useful (0 votes)
7 views

Data Representation Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Representation Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 34

User defined data types, File organization

and access, Real numbers and


normalized floating point representation
3.1.1 – 3.1.2 – 3.1.3
File Organization and Access
Introduction
Computers can store large volumes of data. The difficulty is to be able to get it back. In order
to be able to retrieve data, it must be stored in some sort of order.
Take the phone book for example it contains large volumes of data which can be accessed
fairly easily because all the records are stored in alphabetical order. Imagine how difficult it
would be to find an entry if the records had just been placed in the book at random. The
worth of the book is not just that it contains all the data that may be needed. But that it has
a structure that makes it easily accessible. Similarly, the structure of the data in a computer
file is just as important as the data that it contains. There are a number of ways of arranging
the data that will aid access under different circumstances.

The three ways in which data is stored are:


 Serial
 Sequential
 Random (Direct)
Serial Organization
Data is stored in the computer in the order in which it arrives (chronological order). This is
the simplest form of storage, but the data is much unstructured, so finding a data item again
can be very difficult.

This sort of data storage is only used when it is unlikely that the data will be needed again, or
when the order of the data is not critical.

A good example of a serial file is the presentation you are reading right now. The characters
were all typed in, in a random pattern and less order. Reading this presentation would be
close to impossible if all the words were in alphabetic order!

Another example in this regard would be movies in cinemas or TV shows that we all love to
watch. They play in a certain order in which they are recorded or being put in a playlist in
which their order and time are being calculatedly placed.
Serial Organization
When thinking of storage systems, one could presume that all of your data in one folder is
located next to each other within the hard drive.

This is false when talking about random access. With random access, your information can
be pulled from any location on the disk. Meaning, your one folder could have its data
scattered about the physical hard drive. The benefit of this type of storage is that you could
access data in any order.

Think of it as your CD player, your favorite song ends and you want to hear it again just hit
the back button and done. It’s fast and nearly instantaneous, unlike sequential.

You could think of sequential access like a cassette within a cassette player. When a song
finishes and you want to listen to it again, you must rewind the cassette, or if you want to
skip a song, you must fast forward the tape. This is used with magnetic tape drives which are
used for backup purposes.
Serial Organization
Even though in random access media devices it may seem like data could be misplaced or
somehow lost in the sea of data. When created, every file is given a unique name by the computer
system, otherwise referred to as addressable media, in order to keep track of all the data so it can
be accessed later.

Random access and sequential access of data are two separate ways a computer can access data.
Random access is the ability to access data in any given location within the hard drive, quickly and
efficiently. Most computers use random access today because it saves the user time.

Sequential access requires data being accessed in a sequence. Examples of sequential access
would be:
 Data on a disk file
 Magnetic tape disk storage

This can be useful to some users, if they are purposely attempting to process a sequence of data
elements in order. However, this can also be time consuming for users who are trying to find a
certain file on a disk tape, which requires skimming through all of the data in sequence.
Serial Organization
An example for comparison between random and sequential access would be the A-Z
method. Sequential access would inquire the user to go through letters A-Z to achieve the
goal of meeting the letter you desire. Whereas with random access, the user is able to jump
directly to point “Z”.
Serial Organization

The alternative access method is direct-access which allows records to be read and written
in any order. Most systems only permit this for files stored on random-access devices such as
discs; it is sometimes also permitted on tapes. All records in a direct-access file must be the
same length so that the system can compute the location of a record from its record number
unlike sequential access where variable length records are allowed. The record length has to
be chosen when the file is created and (on most systems) is then fixed for the life of the file.
Sequential Organization

Consider a set of students whose records are stored in a computer.


The data could have been stored in alphabetic order of their
names. It could have been stored in the order that they came in a
Computing exam, or by age with the oldest first. However it is
done, the data has been arranged so that it is easier to find a
particular record. If the data is in alphabetic order of name and the
computer is asked for “Zaid” record, it won’t start looking at the
beginning of the file, but at the end, and consequently it should
find the data faster.

A file of data that is held in sequence is known as a sequential file


Random Organization

A file that stores data in no order is very useful because it makes


adding new data or taking data away very simple. In any form of
sequential file, an individual item of data is dependent on other
items of data. ‘Jawad’ cannot be placed after ‘Mahmood’ because
that is the wrong ‘order’. However, it is necessary to have some
form of order because otherwise the file cannot be read easily.
What would be wonderful is if, by looking at the data that is to be
retrieved, the computer can work out where that data is stored. In
other words, the user asks for Jawad’s record and the computer
can go straight to it because the word Jawad tells it where it is
being stored.
Data Representation
User Defined Data Types
You have already studied a variety of built-in datatypes such as: integers, strings, chars and
more. But often these limited datatypes fail to meet the programmer’s demand which forces
him/her to build their own datatypes. Just as an integer is restricted to "a whole number from
-2,147,483,648 through 2,147,483,647", user-defined datatypes have boundaries places
according to the programmer’s need.

There are 2 categories of user defined data types:


Composite
 Set
 Record
 Class/Object
Non-composite
 Enumerated (enum)
 Pointers
Non-Composite Data Types
Enumerated
If you are using lots of constants in your program that are all related to each other, then it is
a good idea to keep them together using a structure called an “Enum”. For example, you
might want to store the names of each set of cards in a deck.

Here’s one way of writing that in code:

Const heart as integer = 1


Const club as integer = 2
Const spade as integer = 3
Const diamond as integer = 4
Dim cardset as string
Cardset = spade
Non-Composite Data Types
Enumerated
We can bring them all together in a nice organized enum:
Enum suits
hearts = 1
clubs = 2
spades = 3
diamonds = 4
End Enum
Dim cardset as suits
Cardset = suits.hearts
Non-Composite Data Types
Enumerated
This allows you to set meaningful names to the enum and its members, making it much easier to remember
as well as making the code easier to read.
We may also create separate constants to store the points of a football match
Const Win as integer = 3
Const Draw as integer =1
Const Lose as integer = 0
With enums, we could create a datatype called “Result” and store the points within it, under an easy to
remember name.
Enum Result
Win = 3
Draw = 1
Lose = 0
End Enum
Dim ManUvChelsea as Result
ManUvChelsea = Result.Win
Non-Composite Data Types
Pointers
A pointer is a variable that represents the location of a particular data item within a specified domain
(such as the hard drive, or an array). Within the computer’s memory, every stored data item
occupies one or more contiguous memory cell/s. The number of memory cells needed to store a data
item depends on the data type of that item.

For example, a single UcharacterU will typically be stored in 1 byte of memory; an integer usually
requires 4 contiguous bytes, a floating-point number usually requires 4 contiguous bytes, and so
on…

To understand pointers, you must first understand how data is stored in the computer’s memory. In
typical computer architecture of the memory, each byte of the memory has a unique address. Let us
assume the first byte has an address of “201”. Then the next address will be “202” and we’ll go on
“203”,”204” and so on…

Now when we initialize a variable, the computer allocates some amount of memory corresponding to
this particular variable depending on the data type of the variable. So for example ‘a’ as type integer
Non-Composite Data Types
Pointers

And the computer has an internal structure called a lookup table where it stores data such
as the variable name, its data type, and where it resides in the memory. Now if we declare
another variable for instance, character ‘c’. Once again when the machine sees this
declaration, it knows that it is a character variable, so it looks for a free byte and places ‘c’
over there. In this case stored at ‘202’
Non-Composite Data Types
Pointers
Now the main question is, can we know the address of a variable in our program? Yes we
can! Using the concept of “pointers”. We can have another variable, type of which is a
pointer ‘p’. Now this variable ‘p’ can store the address of ‘a’. ‘p’ also takes some memory, so
let’s say that it’s stored at location address 64 and it also takes 4 bytes of memory.
Non-Composite Data Types
Pointers
There is one more important feature of pointers. If we put as asterisk (*) in front of the
pointer when declaring the variable, then it gives us the value of the variable that it points
to. This concept is known as “de-referencing”

‘p’ <points to> address


‘*p’ <points to> value at address
Composite Data Types
Set:
It is a composite data type that can store data in any particular order, in which each element
is indexed. Sets cannot have data items repeated more than once
Constructing Sets
One way to construct sets is by passing any sequential object to the "set" constructor.

We can also add elements to sets one by one, using the "add" function.
Composite Data Types
The set function also provides a copy constructor. However, remember that the copy
constructor will copy the set, but not the individual elements.

Membership testing
We can check if an object is in the set using the same "in" operator as with sequential data
types.
Composite Data Types
We can also test the membership of entire sets. Given two sets S1 and S2, we check if S1 is a
subset or a superset of S2.

Removing items
There is the “remove” function to remove a specified element from the set.

However, removing an item which isn’t in the set causes an error.


Composite Data Types
Intersection
Any element which is in both S1 and S2 will appear in their intersection.

Union
The union is the merger of 2 sets. Any element in S1 or S2 will appear in their union.

Set Difference
It will return all the elements that are in S1 but not in S2
Composite Data Types
Frozenset
A frozenset is basically the same as a set, except that its members cannot be changed. This
means that they can be used as members in other sets. frozensets have the same functions
as normal sets, except the functions which change the contents (add, remove, update, etc.)

Record
A record is a value that contains other values, indexed by names.
Field – an element of a record
Records are collections of data items (fields) stored about something. They allow you to
combine several data items (or fields) into one variable. An example at your college they will
have a database of records for each student. This student record would contain fields such as
ID, Name, and Date of Birth.
Composite Data Types
Example
Composite Data Types
Example Code Output

This code helps store data about a student in record form, however after entering the data of
one student, the program will terminate. How can we edit this code to allow for multiple
records to be added?
Composite Data Types
One way of doing this is defining more fields to accommodate more data entry. i.e. 3 fields
for each student

It would take an awfully long time to declare them all, as well as writing data in them. So how
do we solve this? Well we need to combine two things we’ve already learnt so far, the record
and the array. We are going to make an array of student records.
Composite Data Types
Composite Data Types

Exercise 1: Declare a record called “player” to store the following Role Playing Game
attributes: health, name, class (barbarian, wizard, elf), gold, gender

Exercise 2: Create 2 characters, Gandolf and Conan using the player record
Composite Data Types
Answer to Exercise 1
Composite Data Types
Answer to Exercise 2
Composite Data Types
Class/Object :
In object-oriented programming, a class is a construct that is used as a blueprint (or
template) to create objects of that class. This blueprint describes the state and behavior that
the objects of the class all share. An object of a given class is called an instance of the class.
All the instances of a class have similar properties. For example, you can define a class called
“Car” and create three instances of the class “Car” for “Polo”, “Mini” and “Beetle”.
Composite Data Types
Structures (Records) are very similar to Classes in that they collect data together. However,
classes extend this idea and are made from two different things:
Let’s take a look at the following example:
Composite Data Types
You can see that the class is called ‘car’ and it has:
 2 attributes:
 maxSpeed
 fuel
4 methods
 3 procedures:
 setSpeed
 refuel
 drive
 1 function:
 getSpeed

Remember this is a class and therefore only a template, we need to ‘create’ it using an
object.
Composite Data Types
Attributes
These store information about the object. In the example above we store the fuel and
maxSpeed. The attributes are attached to the classes, and if there are several instances
(objects) of the classes then each will store its own version of these variables. The terms
‘private’ and ‘public’ are substitutes of the term ‘dim’ and belongs to another section in OOP.

Methods
Unlike structures, OOP allows you to attach functions and procedures to your code. This
means that not only can you store details about your car (attributes), you can also allow for
sub routines such as ‘drive()’ and ‘refuel’. Which are attached to each class.
Composite Data Types
KEY Points to remember:

What is the difference between a class and an object?


A class is a template which cannot be executed
An object is an instance of a class which can be executed
One class can be used to make many objects
What are the main components of a class?
Attributes
Methods
What is the difference between a structure and a class?
Structures do not have any methods

You might also like