+ All Categories
Home > Documents > 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined...

2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined...

Date post: 09-Jul-2020
Category:
Upload: others
View: 16 times
Download: 0 times
Share this document with a friend
25
CPT2: Programming Techniques and Logical Methods DJM 31 ADDITIONAL NOTES: 2.3 DATA TYPES AND DATA STRUCTURES Data types Candidates should be able to: 20. Define different data types, e.g. numeric (integer, real), Boolean, character and string; select and use them appropriately in their solutions to problems. A data type is a method of interpreting a pattern of bits. Intrinsic data types Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types. They are used to make the storage and processing of data easier and more efficient. Different databases and programming systems have there own set of intrinsic data types, but the main ones are: Integer; Real; Boolean; String; Character; Date; Container. Integer An integer is a positive or negative number that does not contain a fractional part. Integers are held in pure binary for processing and storage. Note that some programming languages differentiate between short and long integers (more bytes being used to store long integers). In REALbasic an integer uses 4 bytes of memory – this allows it to take a whole number value between ± 2, 147, 483, 648.
Transcript
Page 1: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 31

ADDITIONAL NOTES:

2.3 DATA TYPES AND DATA STRUCTURES Data types

Candidates should be able to: 20. Define different data types, e.g. numeric (integer, real),

Boolean, character and string; select and use them appropriately in their solutions to problems.

A data type is a method of interpreting a pattern of bits.

Intrinsic data types Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types. They are used to make the storage and processing of data easier and more efficient. Different databases and programming systems have there own set of intrinsic data types, but the main ones are:

• Integer; • Real; • Boolean; • String; • Character; • Date; • Container.

Integer An integer is a positive or negative number that does not contain a fractional part. Integers are held in pure binary for processing and storage. Note that some programming languages differentiate between short and long integers (more bytes being used to store long integers). In REALbasic an integer uses 4 bytes of memory – this allows it to take a whole number value between ± 2, 147, 483, 648.

Page 2: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

32 DJM

ADDITIONAL NOTES:

Real A real is a number that contains a decimal point. In many systems, real numbers are referred to as singles and doubles, depending upon the number of bytes in which they are stored.

In REALbasic, a single uses 4 bytes of memory and can have a value between –1.175494 e-38 and 3.402823 e+38; a double uses 8 bytes of memory and can have a value between 2.2250738585072013 e-308 and 1.7976931348623157 e+308. r.

Boolean A boolean is a data-type that can store one of only two values – usually these values are True or False. Booleans are stored in one byte – True being stored as 11111111 and False as 00000000. Many of the properties of objects (windows, text-boxes, buttons etc.) in REALbasic have Boolean values – e.g. the visibility of an object will be set to either True (visible) or false (Hidden). These values are often set using check-boxes.

String A string is a series of alphanumeric characters enclosed in quotation marks. A string is sometimes just referred to as ‘text’. Any type of alphabetic or numeric data can be stored as a string: “Birmingham City”, “3/10/03” and “36.85” are all examples of strings.

Each character within a string will be stored in one byte using its ASCII code; modern systems might store each character in two bytes using its Unicode.

The maximum length of a string is limited only by the available memory. Notes:

• if dates or numbers are stored as strings then they will not be sorted correctly; they will be sorted according to the ASCII codes of the characters – “23” will be placed before “9”;

• telephone numbers must be stored as strings or the leading zero will be lost.

Page 3: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 33

ADDITIONAL NOTES:

Character A character is any letter, number, punctuation mark or space, which takes up a single unit of storage (usually a byte).

Dates In most computer systems dates are stored as a ‘serial’ number that equates to the number of seconds since January 1st, 1904 (thus they also contain the time). Although the serial numbers are used for processing purposes, the results are usually presented in one of several ‘standard’ date formats – for example, dd/mm/yyyy, or dd MonthName, yyyy.

Dates usually take 8 bytes of storage.

Container A container is a data-type used for storing images, video, sound or another type of ‘complex’ file.

Note that in MySQL databases the term ‘blob’ (binary large object) is used rather than ‘container’

Comparison of the common data types Data Type Example value Storage Required

Integer 42 4 bytes

Real 23.1 4 or 8 bytes

Boolean True 1 byte

String “Hello World” 1 byte for each character (if using Unicode, then 2 bytes are required for each character)

Character “X” 1 byte

Date 12/11/2009 8 bytes

Container Image, video or sound Lots!

Page 4: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

34 DJM

ADDITIONAL NOTES:

User-defined data types At times it is useful for programmers to be able to define their own data types. In such cases it is common for these user-defined data types to contain items from several of the different intrinsic types mentioned above. Some programming languages allow the programmer to do this by defining ‘Records’. Benefits of defined data-types Whether intrinsic or user-defined, the use of data-types within a programming language:

• enable the compiler to reserve the correct amount of memory for the data – e.g. 4 bytes for an integer;

• trap errors that a programmer has made and errors that a user of a program can make – a variable defined as an integer cannot be given a fractional value;

• restrict the values that can be given to the data – a Boolean cannot be given the value “maybe”;

• restrict the operations that can be performed on the data – a string cannot be divided by 10.

Page 5: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 35

ADDITIONAL NOTES:

Data structures

Candidates should be able to: 21. Define and use arrays (one- and two-dimensional) for solving

simple problems, including initialising arrays, reading data into arrays and performing a simple serial search on a one-dimensional array.

A data structure is a collection of different data items that are stored together in a clearly defined way. Two common data structures are arrays and records.

Array An array is a data structure, which allows a set of items of identical data type to be stored together using the same identifier name. Arrays are declared in a similar way to standard variables, except that the array size and dimensions are included. For example, the following declares an array that reserves five locations in memory and labels these as ‘Names’:

DIM Names(4) As String

The five individual locations are Names(0), Names(1), Names(2), Names(3), Names(4).

Each data item is called an element of the array. To reference a particular element a programmer must use the appropriate index. For example, the following statement assigns data to the 5th element:

Names(4) = “Johal”

Arrays simplify the processing of similar data. An algorithm for getting five names from the user and storing them in the array Names is shown below:

Dim Names(4) As String For i=0 to 4 Input Value Names(i)=Value Next i

Page 6: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

36 DJM

ADDITIONAL NOTES:

One-dimensional arrays A one-dimensional array is a data structure in which the array is declared using a single index and can be visually represented as a list. The following diagram shows the visual representation of the array Names(4):

Two-dimensional arrays A two-dimensional array is a data structure in which the array is declared using two indices and can be visually represented as a table. The following diagram shows the visual representation of an array Students(4,2):

Each individual element can be referenced by its row and column indices. For example:

Students(0,0) is the data item “JONES” Students(2,1) is the item “M” Students(1,2) is the item “LAMBOURNE”

Page 7: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 37

ADDITIONAL NOTES:

Initialising an array Initialising an array is a procedure in which every value in the array is set with starter values – this starting value would typically be “” for a string array, or 0 for a numeric array. Initialisation needs to be done to ensure that the array does not contain results from a previous use, elsewhere in the program. Algorithm for initialising a one-dimensional numeric array:

DIM TestScores(9) As Integer DIM Index As Integer FOR Index = 0 TO 9 TestScores(Index) = 0 NEXT

Algorithm for initialising a two-dimensional string array:

DIM Students(4,2) As String DIM RowIndex, ColumnIndex As Integer FOR RowIndex = 0 TO 4 FOR ColumnIndex = 0 TO 2 Students(RowIndex,ColumnIndex) = “” NEXT NEXT

Page 8: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

38 DJM

ADDITIONAL NOTES:

Serial search on an array The following pseudo-code can be used to search an array to see if an item X exists:

01 DIM Index As Integer 02 DIM Flag As Boolean 03 Index = 0 04 Flag = False 05 Input X 06 REPEAT 07 IF TheArray(Index) = X THEN 08 Output Index 09 Flag = True 10 END IF 11 Index = Index + 1 12 UNTIL Flag = True OR Index > Maximum Size Of TheArray

Note that the variable Flag (line 04 and 09) is used to indicate when the item has been found and stop the loop repeating unnecessarily (line 12 ends the loop if Flag has been set to True). To complete the search algorithm some lines should be added, after the loop, to detect the times when the item X was not found in the array:

13 IF Flag = False THEN 14 Show Message “Item not found” 15 END IF

Page 9: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 39

ADDITIONAL NOTES:

Using an array with differing data types If an array is to be used to store data of different data types, then:

1. The different data must be defined within a Record:

RECORD Student Name: String Gender: Character Age: Integer END RECORD

2. The array may now be declared to contain items of data type Record:

DIM MyArray(6) As Record

3. Each record can now be stored as a single item within the array.

Page 10: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

40 DJM

ADDITIONAL NOTES:

Relative advantages of the different data types

Candidates should be able to: 22. Explain the advantages and disadvantages of different data

types and data structures for solving a given problem.

COMMENTS TO BE ADDED HERE

Page 11: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 41

ADDITIONAL NOTES:

Records

Candidates should be able to: 23. Design and implement a record format.

Key terms The following terms are used to describe parts of a database:

Field A field is a single category of data within a database, which appears in all the records of a table – it is a column within a table.

Record A record is a collection of fields that contains data about a single item or person – it is a row within a table.

Table A table is a collection of related records.

Page 12: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

42 DJM

ADDITIONAL NOTES:

Key fields A key field is used to identify the records within a database. There are two types of keys:

• primary key; • secondary key.

Primary key The Primary key is a unique field that identifies a single record. Some ‘natural’ primary keys are:

• CarRegistrationNumber; • ISBN – a 10-digit code that uniquely identifies a book; • MAC number – a 6-part number that uniquely identifies a network card • NationalInsuranceNumber – can uniquely identify employees of a company (not usable for

under 16s or for non-British nationalists!)

Secondary key A Secondary key is a non-unique field, used in a search, that does not always produce only one matching record. Some typical secondary keys are:

• LastName; • PostCode; • DateOfBirth

Page 13: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 43

ADDITIONAL NOTES:

Modes of file access

Candidates should be able to: 24. Define different modes of file access: serial, sequential,

indexed sequential and random; and justify a suitable mode of file access for a given example.

Serial file A serial file is one in which records are stored, one after the other, in the order in which they are added – not in order of a key field. This means that new records are stored at the end of the file. The following shows a serial file that is used to store the number of entries for EdExcel GCSE Mathematics. The entries were received in the order: Kettlewood, Queens Park, St Mary’s, Wilton High, West Orling.

Centre Number

Centre Name

No of Candidates

27102 Kettlewood 85

38240 Queens Park 103

64715 St Mary’s 121

30446 Wilton High 156

12304 West Orling 105

Note that the key field in this file would be Centre Number (it uniquely identifies each school) Both disks and tapes can be used to store a file serially.

Page 14: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

44 DJM

ADDITIONAL NOTES:

Sequential file A sequential file is one in which the records are stored, one after the other, in the order of the key field. The following shows a sequential file that is used to store the number of entries for EdExcel GCSE Mathematics. The entries were added in the order: Kettlewood, Queens Park, St Mary’s, Wilton High, West Orling but they are stored in the order of the key field – Centre Number:

Centre Number

Centre Name

No of Candidates

12304 West Orling 105

27102 Kettlewood 85

30446 Wilton High 156

38240 Queens Park 103

64715 St Mary’s 121

As with a serial file, both tape and disks can be used to store a file sequentially and access to the records must take place from the beginning of the file.

Benefits Sequential files allow the records to be displayed in the order of the key field – this makes the process of adding a record slower, but significantly speeds up searches.

Page 15: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 45

ADDITIONAL NOTES:

Indexed sequential file An indexed sequential file is one in which the records are stored, one after the other, in the order of the key field, but which also has an index that enables records to be accessed directly.

Index An index is a file with two fields, created from the main file, which contains a list of:

• the key fields (sorted sequentially); • pointers – to where the records can be found in the main file.

Indexed sequential files are useful when:

• it is sometimes necessary to process all the records in sequential order; and • it is sometimes necessary to access individual records randomly.

Examples of indexed sequential files

Company employee file At the end of each month all the records will be processed sequentially, in order to produce payslips. However, some records will need to be accessed randomly, at other times – for example, when an employee changes address. A school’s student file When an attendance report is printed, the file will be accessed sequentially, but when the details of an individual student are required the index will be used to find the required record quickly.

Page 16: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

46 DJM

ADDITIONAL NOTES:

Random (direct) access file A random access file is one in which a record can be written or retrieved without first examining other records. A random access file must be stored on disk and the disk address is calculated from the primary key. In its simplest form a record with a primary key of 1 will be stored at block 1, a record with a primary key of 2 will be stored at block 2; a record with primary key 3 will be stored at block 3 etc:

It should be noted that this very simple method where [disk address] = [primary key] is very inefficient in respect of disk space. For example:

• if the lowest primary key is 1001, then all the disk space below block 1001 will be wasted. • If there are some values which the primary key never takes (for example odd values) – these

storage spaces will be wasted. In order to be more efficient with the use of disk space, random access files calculate disk addresses by using a hashing algorithm (also known as just hashing).

Page 17: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 47

ADDITIONAL NOTES:

Hashing Hashing is a calculation that is performed on a primary key in order to calculate the storage address of a record. A hashing algorithm will typically divide the primary key by the number of disk blocks that are available for storage, work out the remainder and add the start address. The answer will be the storage address of the record.

[disk address] = [primary key] MOD [number of blocks] + [start address]

Example If a file was to be stored on the first 5000 blocks of a disk then:

[disk address] = [primary key] MOD 5000

That is, the primary key of each of the records would be divided by 5000 and the remainder would be the disk address for the record.

This means that a record with primary key of 27102 would be stored at the disk address calculated as follows:

271025000

= 5 remainder 2102

This means that the disk address for this record will be 2102.

The table shows some other disk addresses calculated using the same hashing algorithm:

Centre Number

Centre Name

No of Candidates

Disk Address

27102 Kettlewood 85 2102

38240 Queens Park 103 3240

64715 St Mary’s 121 4715

30446 Wilton High 156 446

12304 West Orling 105 2304

Page 18: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

48 DJM

ADDITIONAL NOTES:

Problems with hashing One problem that could occur with hashing is that a block may already contain a record and be full. For example records with key fields of 38240 and 43240 will both be assigned a disk address of 3240.

If this happens then the new record will need be written somewhere else. Two common ways of determining this alternative location are:

• the record can be written to the next available block – note that if it is the last address block which is full then the search for an available space will start from the first block.

• the record could be written to a separate ‘overflow’ area and a tag is placed in the calculated location to indicate exactly where in this overflow area the record can be found.

Page 19: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 49

ADDITIONAL NOTES:

Exercise CPT2.2B

1. (a) What is meant by a serially organised file? [2] (b) State two characteristics of a sequentially organised file. [2]

2. Details of each new loan in a library can only be added to the end of the loans file. What type

of file organisation does the loans file use? [1]

3. If a program is written which searches a sequential file for a match with a given word that might be recorded in the file, what are the steps for this process? [4]

4. (a) What file organisation is required if the priority is to find a specific record in a file?

Justify your answer. [2] (b) What file organisation is required if the priority is to access individual records quickly?

Justify your answer. [2]

5. List the steps required in order to add a record to a sequential file. [4]

6. What is a direct access medium? [2]

7. (a) What is meant by ‘hashing’? [2] (b) Give two properties that a hashing algorithm should have. [2]

(c) A hashing algorithm uses the expression (ASCII Code)Mod 150. Why is Mod 150 used? [1]

Page 20: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

50 DJM

ADDITIONAL NOTES:

8. (a) What are the major steps that a typical hashing function/algorithm would use to convert a alphanumeric string into a two-byte integer. [2]

(b) Why is the hashing function used for passwords on a LAN, a one-way (irreversible) function? [1]

9. List the programming steps required to generate a direct access file by using a hashing

algorithm on a sequential file. Note that details of how a hashing algorithm works are not required. [3]

10. When a member of staff logs onto a computer in a particular organisation, they enter there

username and password. The password is input to the logon program as an alphanumeric string and converted to a two-byte integer using a hashing function or algorithm before being sent across a network or authentication.

Outline three major steps that a typical hashing function/algorithm would use to convert an alphanumeric string into a two-byte integer. [3]

Page 21: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 51

ADDITIONAL NOTES:

Handling data within files

Candidates should be able to: 25. Store, retrieve and search for data in files; 26. Use the facilities of a procedural language to perform file

operations (opening, reading, writing, updating, inserting, appending and closing) on files of different access modes as appropriate.

Before a program can use a file, the file needs to be opened. The program needs to specify whether the file is to be opened for writing, or opened only for reading. After the data has been read or written to the file, it then needs to be closed. All algorithms for handling data within files must have the following lines:

OPEN [New/Existing] File in READ/WRITE MODE … … CLOSE All Files

Note that these lines are not necessarily at the beginning and the end of the code, but they must be in place to make sure that the file(s) is opened and closed correctly.

Adding data

Serial file Adding data is simple – it is added to the end of the file:

OPEN File in WRITE MODE GOTO End of File WRITE NewData CLOSE File

Page 22: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

52 DJM

ADDITIONAL NOTES:

Sequential file The addition of data to a sequential file is more complicated than in a serial file, because the record must be inserted into the correct position – not just at the end of the file. In practise, when a record is added to a sequential file, it is usual to create a new file and copy all the records from the old file, but insert the new record in its correct position. An algorithm for this is shown below:

OPEN a NewFile in WRITE MODE OPEN ExistingFile in READ MODE READ First Record in ExistingFile REPEAT IF key of SelectedRecord in ExistingFile < key of NewRecord THEN COPY SelectedRecord into NewFile ELSE COPY NewRecord into NewFile COPY SelectedRecord into new file END IF READ Next Record in ExistingFile END REPEAT when new record has been copied COPY ALL remaining records from ExistingFile into NewFile CLOSE NewFile and ExistingFile

Random file The hashing algorithm is applied to the Key field in order to find the storage location for the new data.

OPEN File in WRITE MODE READ KeyField of NewRecord Apply Hashing Algorithm to calculate DiskAddress WRITE NewRecord to calculated DiskAddress CLOSE File

Page 23: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 53

ADDITIONAL NOTES:

Searching for/retrieving data

Serial file To retrieve data from a serial file, a program must examine the first record and then all subsequent records until the desired one is found or until the end of the file has been reached. The following algorithm does this:

OPEN File in READ MODE READ First Record SET Variable Found = False REPEAT IF RequiredRecord = SelectedRecord THEN SET Variable Found = True ELSE READ Next Record END IF END REPEAT when Found = True OR when EOF is reached CLOSE File

Note that to be sure that a record does not exist in a serial file, every single record must be examined.

Sequential file Searching a sequential file is the same as searching a serial file, except that the search only has to continue until a record with a higher Key field is reached – this would mean that the data is not in the file.

OPEN File in READ MODE READ First Record SET Variables Found = False, Exists = True REPEAT IF RequiredRecord = SelectedRecord THEN SET Variable Found = True ELSE READ Next Record IF Key of RequiredRecord > Key of SelectedRecord THEN Exists = False END IF END IF END REPEAT when Found = True OR Exists = False OR when EOF is reached CLOSE File

Page 24: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

54 DJM

ADDITIONAL NOTES:

Random file

OPEN File in READ MODE READ KeyField of RequiredRecord Apply Hashing Algorithm to calculate DiskAddress READ Record at DiskAddress IF Record does not exist THEN OUTPUT “Record Not Found” END IF CLOSE File

Deleting data

Serial or sequential file

OPEN a NewFile in WRITE MODE OPEN ExistingFile in READ MODE READ First Record in ExistingFile REPEAT IF Key of SelectedRecord <> Key of RecordToDelete THEN COPY SelectedRecord into NewFile ELSE DO NOT COPY SelectedRecord END IF READ Next Record in ExistingFile END REPEAT when EOF is reached CLOSE NewFile and ExistingFile DELETE ExistingFile RENAME NewFile to Name of ExixtingFile

Page 25: 2.3 DATA TYPES AND DATA STRUCTURES€¦ · Intrinsic data types are the data types that are defined within a particular programming language. There are numerous different data types.

CPT2: Programming Techniques and Logical Methods

DJM 55

ADDITIONAL NOTES:

Calculating file size

Candidates should be able to: 27. Estimate the size of a file from its structure and the number of

records.

The basic formula for estimating the size of a file is:

Size of file = [size of each record] × [number of records] + [a little bit more!]

If we consider a file with 200 records, which stores the details of an organisation’s customers:

CUSTOMER(RefCode, Name, PostCode, Telephone, DoB, Age)

We can estimate the size of the record as follows: Attribute Data type Extreme Example Size of field

(bytes)

RefCode Integer 99 999 4

Name String Margaret Edwards 20

PostCode String WC12 16AA 9

Telephone String (0203) 9898 1234 16

DoB Date 31-12-76 8

Age Real 104 4

Total 62

Thus 200 records would require:

62 × 200 = 12400 bytes

= 124001024

Kbytes

= 12.1+1.21 (10%)= 13.3 Kbytes

Note that to determine the maximum field length, an extreme case was considered and several bytes added to play safe.


Recommended