Data Types
Definition
A data type is a formal description of the kind of data used in a computer program.
Why do we need data types?
How an item of data is stored an manipulated within a program depends on it's data type. It allows the computer to allocate memory according to data type
Declaring variables
This is when the locations for storing specific data are created and initialised. When declaring a variable there are two bits of information which need to be supplied, in most languages:
An identifier (the unique name for that variable)
The data type of that variable
Common data types
Numeric data types, used to store numbers. The most common of these are integer and real.
Alphanumeric data types, used to store textual data so characters, digits and punctuation
Booleans, data which can only have two values, typically true or false.
Data Structures
Definition
These ate methods of storing more than one item of data together under a single identifier
There are two main ways of doing this, using either records or arrays.
Arrays
An array contains several items of similar data.
An array contains a number of elements, which are the items of data.
They must all be of the same data type.
Therefore they take up the same space in memory, and can be stored next to each other.
An array has an index containing a list of the addresses of all the elements.
The lowest and highest values of the index are the bounds of the array.
It is also possible to have multi-dimensional arrays.
Advantages of arrays:
Declare all the elements in one statement
You can use a loop to access similar data in an array
Records
Records can be used to store several different data items of different types but all related to one thing, like details on a particular customer.
The individual items inside a record are called fields.
File Organisation
How records are organised into files.
Serial file
In a serial file the data is stored in the order it arrives.
New data is added to the end of the file
This is called appending
To delete an item of data:
Recreate the file
copy all the records apart from the one to be deleted
Use new file to replace existing one
To search for an item of data:
Start with the first item
Look at every item in turn
Until you find the record you are looking for
If it's not in the file, you will need to search every item before you can be sure it's not there
Sequential File
The data is stored according to a key field in the data.
A key field in each record can be used to identify that record
To add a new item into a record:
Create new file
Copy all records up to that records position into the new file
Copy the new record in
Copy the rest of the items over
Let the new file replace the old file
Deleting from a sequential file is the same as deleting from a serial file
Indexed Sequential Files
A sequential file where the data is arranged according to a key field in the record, but also has an index that allows records to be found directly.
This is useful where sometimes it is necessary to process all records in sequential order, or if you want to look up an individual record the index can be used.
Direct Access Files
If the records in a sequential file have a fixed length, it can be possible to read the contents of the file from knowing it's position.
Random Files
Where the data can be stored anywhere in a dedicated area of the disk, depending on the result of a calculation called a hash algorithm.
Estimating the Size of a File
Determine the size of each field in the record in bytes
Add the field sizes to calculate the size of one record
Multiply the size of one record by the size of one record by the number of records in the file
Add 10% on for overheads
Divide by 1000 to get kb, 1000 again for mb and so on