13.1, 13.2 User Defined Datatype & File Organi
13.1, 13.2 User Defined Datatype & File Organi
Syllabus Content:
13. Data representation
13.1- User-defined data types
Show understanding of why user-defined types are necessary
Define and use non-composite types
Define and use composite data types
Choose and design an appropriate user-defined data type for a given problem
Notes and guidance
Including enumerated, pointer (Non-Composite)
Including set, record and class / object (composite)
An enumerated data type defines a list of possible values. The following pseudocode
shows two examples of type definitions:
TYPE <identifier> = (value1, value2, value3, …)
These values are ordinal and you can use mathematical and
comparison operators on them.
It is important to note that the values of the enumerated type look like string values but
they are not. They must not be enclosed in quote marks.This makes the second
example much more useful because the ordering can be put to many uses in a
program. For example, a comparison statement can be used with the values and
variables of the enumerated data type:
When you are in a situation to have a number of constants that are logically related to each
other, you can define them together these constants in an enumerator list. An enumerated type
is declared using the enum keyword.
Syntax:
An enumeration data type has a name, an underlying data type, and a set of members.
It is useful when you have a set of values that are functionally significant and fixed.
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:
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:
The main non-composite, derived type is the pointer, a data type whose value refers
directly to (or "points to") another value stored elsewhere in the computer
memory using its address.
It is a primitive kind of reference. (In everyday terms, a page number in a book could
be considered a piece of data that refers to another one). Pointers are often stored in a
format similar to an integer; however, attempting to dereference or "look up" a pointer
whose value was never a valid memory address would cause a program to crash. To
ameliorate this potential problem, pointers are considered a separate type to the type
of data they point to, even if the underlying representation is the same.
As you can see there are three basic steps to using pointers.
This isn't really any different than using a standard variable, and you use pointers in
much the same way as standard variables. The only real difference between the two is
that in a standard variable, you can access the data directly, and with a pointer you
must dereference the pointer to interact with the data.
A pointer references a location in memory, and obtaining the value stored at that
location is known as dereferencing the pointer. As an analogy, a page number in a
book's index could be considered a pointer to the corresponding page; dereferencing
such a pointer would be done by flipping to the page with the given page number and
reading the text found on the indexed page.
Declaration of a variable of pointer type does not require the caret symbol ^ to be used:
A special use of a pointer variable is to access the value stored at the address pointed to. The pointer variable is said
to be 'dereferenced'
A record data type is the most useful and therefore most widely used. It allows the
programmer to collect together values with different data types when these form a coherent
whole.
As an example, a record could be used for a program using employee data. Pseudocode
for defining the type could be:
A particular use of a record is for the implementation of a data structure where one or
possibly two of the variables defined are pointer variables.
Records in VB:
A set is an unordered collection of items. Every element is unique (no duplicates) and
must be immutable (which cannot be changed).However, the set itself is mutable. We
can add or remove items from it. The number of elements in a SET data type can vary,
but no nulls are allowed.
Set can have any number of items and they may be of different types (integer, float,
string etc.).
Assume we wish to create a Set. In PSEUDOCODE the type definition has this
structure.
NOTE: Of the three programming languages, Pascal and Python support the set data type.
The following Python code illustrates that the set data type has operators – union and
intersection- which can be used in two sets:
You can try creating SET and its operations online on below link:
https://www.programiz.com/python-programming/set#operations
Let us consider the following two sets for the following operations.
1. >>> A = {1, 2, 3, 4, 5}
2. >>> B = {4, 5, 6, 7, 8}
Set Intersection
Syllabus Content:
13.1 Data representation
13.2 File organisation and access
file organisation: serial, sequential (using a key field) and random (using record key)
file access: sequential access (serial & sequential files) – direct access (sequential &
random files)
select an appropriate method of file organisation and file access for a given problem
(Sec 13.2)
Sequential Files:
The key difference between a sequential file and a serial file is that it is ordered in a
logical sequence based on a key field. This key is usually the primary key, though
secondary keys may be used as well. Sequential files are therefore files that are sorted
based on some key values.
Sequential files are primarily used in applications where there is a high file hit rate. Hit
rate is a measure of the proportion of the records that is accessed in a single run of the
application. Therefore, sequential files are ideal for master files and batch processing
applications such as payroll systems in which almost all records are processed in a
single run of the application.
Random Files:
In random file organisation, records are stored in random order within the file. Though
there is no sequencing to the placement of the records, there is however, a pre-defined
relationship between the key of the record and its location within the file. In other
words, the value of the record key is mapped by an established function to the address
within the file where it resides. Therefore, any record within the file can be directly
accessed through the mapping function in roughly the same amount of time. The
location of the record within the file therefore is not a factor in the access time of the
record. As such, random files are also known in some literature as direct access files.
To create and maintain a random file, a mapping function must be established between
the record key and the address where the record is held. If M is the mapping function,
then
Hashing
There are various mapping techniques. Some involve using the key field to directly
map to the location with the file, while others refer to some lookup table for the
location. However, the more common method is to employ a hash function to derive
the address.
Hashing is the process of transforming the key value of a record to yield an address
location where the record is stored. In some literatures, it is also known as Key-to-
Address Transformation, Address-Calculation, Scatter Storage, or Randomization.
A hash function generates the record address by performing some simple operations on
the key or parts of the key. A good hashing function should be
quick to calculate
not generate addresses that tend to cluster within a few locations, thus resulting
in frequent collisions.
Refrences
Book: AS and A-level Computer Science by
http://www.bbc.co.uk/education/guides/zjfgjxs/revision/1
https://www.tutorialspoint.com/vb.net/vb.net_constants.htm
https://www.cs.drexel.edu/~introcs/F2K/lectures/5_Scientific/overflow.html
https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/underflow.html