- Created by: vinaysutaria
- Created on: 16-04-15 08:54
3.2.3 Data Types & Data Structures
A data type is a formal description of the kind of data used in a computer program.
3.2.3 Data Types & Data Structures (2)
Data structures allows us to store more than one item of data together.
- Under one identifier (name)
Can be used to access individual items of data.
Two data structures - arrays and records.
Arrays contain several items of data.
Used to group variables of the same 'category'.
Actual data stored in the memory is called an element of the array.
The number after the array is called the index...
3.2.3 Data Types & Data Structures (3)
The highest and lowest values of the index are the bounds.
The advantages of using an array as opposed to separate variables...
- All elements are declared in one statement.
- If number of elements changes, change bound of array in decleration.
- Array contain similiar data; loops can be used to perform similiar operations.
Using the VotesForCandidate array, this is an example of a one-dimensional array.
- A one-dimensional array denotes only row data.
- Multidimensional arrays denotes data in the form of rows and columns.
Records can be used to store several data items under one identifier.
Individual items of data inside the record are called fields.
It helps to know the number of records in a file and the size of the records.
This allows the size of the file to be calculated.
3.2.3 Data Types & Data Structures (4)
The code below defines a student and assigns values to its records...
- In a serial file, data is stored in the order in which it arrives.
- New data is simply added to th end of the file.
- This is known as appending the data.
- To search for data you need to start from the first item.
- By then searching each item in turn, you will find the record you are looking for.
- You need to confirm whether the item you are searching for exists in the file.
- By searching every item in the file, you can clarify that the item cannot be found.
3.2.3 Data Types & Data Structures (5)
In a sequential file, data is stored according to a key field in the data,
The key field is a field in each records that is used to identify the record.
For example, if each student was given a Student ID a sequential file of students would be stored in order of the student ID.
Adding new items into sequential files requires insertion into existing records.
- This is usually achieved by recreating the file.
Searching for an item is similiar to a serial file.
However if you reach an item with a higher 'key value' then you know the item cannot be found, and there is no need to continue the search.
An indexed sequential file is a sequential file where data is arranged according to a key field, but which also has an index that allows records to be found directly.
For example, if a program printing a register from a student file, it will access the data sequentially. But if you want to look up the details of an individual student, then the index is used to find the required records quickly.
3.2.3 Data Types & Data Structures (6)
Random files (not to be confused with random-access files) allow data to be stored anywhere in a dedicated section of a disk.
A hash algorithm (calculation) is performed on a field in the record to be stored.
- There is no need of this calculation then becomes the address.
As a result, records can appear randomly scattered across the disk.
Finding data is extremely quick as long as you know the key field and hash algorithm.
- As result random files are very useful for large databases where individual records are often looked up individually.
3.2.3 Data Types & Data Structures (7)
How can we estimate the size of a file?
1. Determine the size (in bytes) of each field in the record.
2. Add the field sizes to calculate the size of one record.
3. Multiply the size of one record by the number of records in the file.
4. Add 10% to the result for additional storage needed to manage the file on disk
- This is called an overhead
- Obviously this will give an estimate in bytes.
- If necessary it can be converted into kilobytes ( divide by 1024).
- It can then be converted again into megabytes ( divide by 1024 again).
3.2.3 Data Types & Data Structures (8)
Supposing a school has 1300 students...
The size of each field can be given by...
- Total size of each record is 23 bytes.
- Size of 1300 records is therefore 23 x 1300 = 29,900 bytes.
- Adding 10% for overheads gives us 29,900 x 1.1 = 32,890 bytes.
- Dividing this by 1000 gives us an estimate of 23.9kB.
3.2.3 Data Types & Data Structures (9)
These are the main file operations - the way they are implemented varies.
PREPARING THE FILE: most languages require the program to make the operating system aware that a file is about to be used. This is done using an OPEN command. When the file is opened it is often necessary to specify how the file is going to be used - this is called access mode. It protects the data in the file from being corrupted. This is why it is hood practice to open a file for as little time as necessary.
When the operation is over, the file should be closed. This releases the file so that it can be used by another part of the program or indeed a different program.
READING DATA FROM A FILE: languages provide different methods for reading data. The simplest way is to read the data from the file one line at a time. This is useful for searching through a serial file. In this case it is necessary to test, before reading each line, that the end of the file has not been reached
3.2.3 Data Types & Data Structures (10)
File operations continued...
WRITING DATA TO A FILE: you also need to be able to write the data into a file that has been correctly opened - this can include adding data to the end of a serial file (appending). Inserting data into a sequential file will require all subsequent records to be moved to make space for the data to be inserted. An alternative method is to create a new file and copy all the records from the present file into it, inserting the new data in the file at the correct location. The new file then replaces the old file. This is called merging the data into the sequential file.
FILE MANAGEMENT: programming languges also provide facilities for copying, moving, updating, deleting files and finding out if a file exists.