Data Representation Notes
Data Representation Notes
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
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”
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.
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: