User Defined Data Types

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

1

1 (a) A particular programming language allows the programmer to define their own data types.

ThisDate is an example of a user-defined structured data type.

TYPE ThisDate
DECLARE ThisDay : (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31)
DECLARE ThisMonth : (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug,
Sep, Oct, Nov, Dec)
DECLARE ThisYear : INTEGER
ENDTYPE

A variable of this new type is declared as follows:

DECLARE DateOfBirth : ThisDate

(i) Name the non-composite data type used in the ThisDay and ThisMonth declarations.

.......................................................................................................................................[1]

(ii) Name the data type of ThisDate.

.......................................................................................................................................[1]

(iii) The month value of DateOfBirth needs to be assigned to the variable


MyMonthOfBirth.

Write the required statement.

.......................................................................................................................................[1]
2

(b) Annual rainfall data from a number of locations are to be processed in a program.

The following data are to be stored:

• location name
• height above sea level (to the nearest metre)
• total rainfall for each month of the year (centimetres to 1 decimal place)

A user-defined, composite data type is needed. The programmer chooses


LocationRainfall as the name of this data type.

A variable of this type can be used to store all the data for one particular location.

(i) Write the definition for the data type LocationRainfall.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[5]
3

2 (a) A particular programming language allows the programmer to define their own data types.

An example of a user-defined data type for an address is:

TYPE ThisAddress
DECLARE ThisHouseNo : INTEGER
DECLARE ThisStreet : STRING
DECLARE ThisTown : STRING
ENDTYPE

A variable of this new type is declared as follows:

DECLARE HomeAddress : ThisAddress

(i) Write the statement that assigns the house number 34 to HomeAddress.

.......................................................................................................................................[1]

(ii) The type definition for ThisAddress is to be changed.

Rewrite one line from the definition for each of the following changes.

House numbers are in the range from 1 to 10.

DECLARE ...........................................................................................................................

The possible towns are limited to: Brightown, Arunde and Shoram.

DECLARE .......................................................................................................................[2]
4

(b) Temperature data from a number of weather stations are to be processed by a program.

The following data are to be stored:

• weather station ID (a unique four-letter code)

• latitude (to 2 decimal places)

• average temperature (to the nearest whole number) for each year from 2001 to 2015
inclusive

A programmer designs a composite data type WeatherStation. A variable of this type can
be used to store all the data for one particular station.

(i) Write the definition for the user-defined data type WeatherStation.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[5]
5

3 (a) Consider the following user-defined data type:

TYPE LibraryBookRecord
DECLARE ISBN : INTEGER
DECLARE Title : STRING
ENDTYPE

(i) Write a pseudocode statement to declare a variable, Book, of type LibraryBookRecord.

.......................................................................................................................................[1]

(ii) Write a pseudocode statement that assigns ‘Dune’ to the Title of Book.

.......................................................................................................................................[1]

(b) The user-defined data type LibraryBookRecord needs to be modified by adding the
following fields:

• a field called Genre which can take two values, fiction or non-fiction
• a field called NumberOfLoans which can be an integer value in the range 1 to 99

Write the updated version of LibraryBookRecord.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]
6

4 (a) Consider the following pseudocode user-defined data type:

TYPE MyContactDetail
DECLARE Name : STRING
DECLARE HouseNumber : INTEGER
ENDTYPE

(i) Write a pseudocode statement to declare a variable, NewFriend, of type


MyContactDetail.

.......................................................................................................................................[1]

(ii) Write a pseudocode statement that assigns 129 to the HouseNumber of NewFriend.

.......................................................................................................................................[1]

(b) The user-defined data type MyContactDetail needs to be modified by:

• adding a field called Area which can take three values, uptown, downtown or midtown
• amending the field HouseNumber so that house numbers can only be in the range 1 to
499.

Write the updated version of MyContactDetail.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]

(c) A pointer is a variable that stores the address of a variable of a particular type.

Consider the pseudocode on page 3, which uses the following identifiers:

Identifier Data type Description


IPointer ^INTEGER pointer to an integer
Sum INTEGER an integer variable
MyInt1 INTEGER an integer variable
MyInt2 INTEGER an integer variable
7

5 A programmer uses non-composite and composite data types to create a program.

(a) Define the term non-composite data type.

...................................................................................................................................................

...............................................................................................................................................[1]

(b) Describe two different non-composite data types.

Data type 1 ...............................................................................................................................

Description ................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Data type 2 ...............................................................................................................................

Description ................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
[4]

(c) Define the term composite data type.

...................................................................................................................................................

...............................................................................................................................................[1]

(d) Describe two different composite data types.

Data type 1 ...............................................................................................................................

Description ................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Data type 2 ...............................................................................................................................

Description ................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
[4]
8

6 Data types can be defined in a programming language.

The data type, StudentRecord, is defined by the code:

TYPE StudentRecord
DECLARE StudentID : INTEGER
DECLARE StudentFirstName : STRING
DECLARE StudentSurname : STRING
DECLARE StudentDOB : DATE
DECLARE StudentCourse : ARRAY[1:10] OF STRING
ENDTYPE

A variable, CollegeStudent, is declared with the code:

DECLARE CollegeStudent : StudentRecord

(a) Write a pseudocode statement to assign 6539 to the StudentID of CollegeStudent.

...............................................................................................................................................[1]

(b) The type definition for StudentRecord is changed.

(i) Students can take six courses from: Computer Science, Engineering, Science, Maths,
Physics, Chemistry, Music, Drama and English Language.

Rewrite one line from the type definition of StudentRecord to implement the change.

DECLARE ...........................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[2]

(ii) The values for the field StudentID must be between 1 and 8000 inclusive.

Rewrite one line from the type definition of StudentRecord to implement the change.

DECLARE .......................................................................................................................[1]
9

(c) A programmer is asked to write a program to process the assessment data for each student.
Students sit one exam in every course they take.

A composite data type, StudentAssessment, needs to be defined with the following three
fields.

• a student assessment code (a unique code of three letters and two digits)
• the marks for the six exams
• the average mark of the six exams

(i) Write pseudocode to define the data type StudentAssessment.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[4]
10

7 Data types can be defined using pseudocode.

The data type, BicycleRecord, is defined in pseudocode as:

TYPE BicycleRecord

DECLARE BicycleID : INTEGER

DECLARE BicycleAvailable : BOOLEAN

DECLARE BicycleLocation : (RiverSide, BusStation, TrainStation,


TownSquare, Library)

DECLARE DateChecked : DATE

ENDTYPE

A variable, LoanBicycle, is declared in pseudocode as:

DECLARE LoanBicycle : BicycleRecord

(a) Write pseudocode statements to assign 567 to the BicycleID of LoanBicycle and
FALSE to the BicycleAvailable of LoanBicycle.

...................................................................................................................................................

............................................................................................................................................. [2]

(b) The type definition for BicycleRecord is changed.

(i) The definition has been extended to include borrower identification numbers,
BorrowerID, for the last 10 people who borrowed a bicycle. Each identification number
is an integer.

Write the pseudocode statement needed in the type definition of BicycleRecord.

...........................................................................................................................................

..................................................................................................................................... [1]

(ii) The values for the field BicycleID must be between 500 and 599 inclusive.

Rewrite one pseudocode line from the type definition of BicycleRecord to implement
the change.

...........................................................................................................................................

..................................................................................................................................... [1]
11

8 Data types can be defined using pseudocode.

The data type, ComputerRecord, is defined by the following pseudocode:

TYPE ComputerRecord

DECLARE ComputerID : INTEGER

DECLARE ComputerType : (Laptop, Desktop, Tablet)

DECLARE ComputerLocation : (Lab1, Lab2, Lab3, Mobile)

DECLARE DateTested : DATE

ENDTYPE

A variable, SchoolComputer, is declared in pseudocode as:

DECLARE SchoolComputer : ComputerRecord

(a) Write pseudocode statements to assign 1234 to the ComputerID of SchoolComputer


and Lab2 to the ComputerLocation of SchoolComputer.

...................................................................................................................................................

............................................................................................................................................. [2]

(b) The type definition for ComputerRecord is changed.

(i) The definition has been extended to include the student identification numbers,
StudentID, for up to 20 students who can use that computer. Each student identification
number is an integer.

Write the extra line of pseudocode needed in the type definition for ComputerRecord.

...........................................................................................................................................

..................................................................................................................................... [1]

(ii) The values for the field ComputerID must be between 1000 and 1999 inclusive.

Rewrite one pseudocode line from the type definition of ComputerRecord to implement
the change.

...........................................................................................................................................

..................................................................................................................................... [1]
12

9 Data types can be classified as composite or non-composite.

A record is declared of type box using the following pseudocode.

TYPE size = (small, medium, large)

TYPE box

DECLARE volume : size

DECLARE price : REAL

DECLARE colour : STRING

ENDTYPE

DECLARE myBox : ARRAY [1:6] OF box

(a) (i) Identify one composite and three non-composite data types used in the pseudocode.

Composite data type .........................................................................................................

Non-composite data type 1 ...............................................................................................

Non-composite data type 2 ...............................................................................................

Non-composite data type 3 ...............................................................................................


[4]

(ii) Identify the data type in the pseudocode that is enumerated.

..................................................................................................................................... [1]

(b) A box is red, with medium volume and a price of $10.99.

Write pseudocode to store the details of this box in the first element of the array.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [3]
13

10 Data types can be classified as composite or non-composite.

(a) Draw one line from each data type to its correct classification.

Data type Classification

Pointer

Record Composite

Set

Class Non-composite

Integer

[2]

(b) A user-defined data type, timeOfDay, is declared using the following pseudocode.

TYPE timeOfDay = (morning, afternoon, evening, night)

(i) Identify the type of user-defined data type declared and state its classification.

Type ..................................................................................................................................

Classification .....................................................................................................................
[2]

(ii) Write pseudocode to declare the variable session of type timeOfDay.


Assign the value afternoon to the variable session.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..................................................................................................................................... [2]
14

11 (a) Explain why user-defined data types are necessary.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [2]

(b) An organisation stores data about its employees.

• Employee ID is a five-digit number, for example, 01234.


• Employee name is a string, for example, ‘Kiri Moana’.
• Department is one of three values: Sales, Technical, Customer services.
• Salary is an integer value in the range 25 000 to 150 000.

(i) Complete the following pseudocode definition of a user-defined data type to store the
employee data.

TYPE Employee

DECLARE EmployeeID : ....................................................................................

DECLARE EmployeeName : STRING

DECLARE Department : ( .................................................................................

..................................................................................)

DECLARE Salary : 25000..150000

...........................................................................................................................................
[4]

(ii) Write a pseudocode statement to declare a variable, NewEmployee of data type


Employee.

...........................................................................................................................................

..................................................................................................................................... [1]

(iii) Write a pseudocode statement that assigns 02244 to the EmployeeID of


NewEmployee.

...........................................................................................................................................

..................................................................................................................................... [1]

(iv) Employee is an example of a record that is a composite data type.

State two other composite data types.

1 ........................................................................................................................................

2 ........................................................................................................................................
[2]
15

12 (a) State what is meant by a user-defined data type.

...................................................................................................................................................

............................................................................................................................................. [2]

(b) A pseudocode declaration for a user-defined data type for the months of the year is as follows:

TYPE
DECLARE Months: (January, February, March, April, May, June, July,
August, September, October, November, December)
ENDTYPE

(i) Identify this type of user-defined data type.

...........................................................................................................................................

..................................................................................................................................... [1]

(ii) Write a pseudocode statement to declare a variable CurrentMonth of data type


Months.

...........................................................................................................................................

..................................................................................................................................... [1]

(iii) Write a pseudocode statement to assign the value August to the variable
CurrentMonth.

...........................................................................................................................................

..................................................................................................................................... [1]
16

13 Consider the following user‑defined data type.

TYPE Book
DECLARE ISBN : INTEGER
DECLARE Author : STRING
DECLARE Title : STRING
DECLARE Supplier : (Amazone, Stones, Smiths, Blackwalls, Greens,
Coals, Boarders)
ENDTYPE

(a) Name the data type of Book.

...............................................................................................................................................[1]

(b) Name the non‑composite data type used in the Supplier declaration.

...............................................................................................................................................[1]

(c) (i) Write a pseudocode statement to declare a variable, BestSeller, of type Book.

.......................................................................................................................................[1]

(ii) Write a pseudocode statement to assign “John Williams” to the author of


BestSeller.

.......................................................................................................................................[1]

You might also like