Computational Thinking and Data Structures: Question 2

Syllabus 10.1

Structured AS 6 marks

A small music club stores basic information about each upcoming event using a single record for each event: the event's name, its ticket price, and whether a particular member is currently marked as attending.

(a) Write Cambridge pseudocode to define a user-defined data type called EventRecord, with three fields: EventName (a string), TicketPrice (a real number) and Attending (a boolean value). [3]

(b) Write pseudocode to declare a variable called NextEvent of type EventRecord, and then write pseudocode statements that store the following values in its three fields: EventName is "Chess Club Finals", TicketPrice is 2.50, and Attending is TRUE. [2]

(c) Write a single pseudocode statement that would output the value stored in the TicketPrice field of NextEvent, and state the value that this statement would output for the data given in part (b). [1]

Show worked solution Hide worked solution

Worked solution

Part (a): Defining the EventRecord data type

A record structure groups several related fields of (possibly) different data types under one identifier. In Cambridge pseudocode, a user-defined record type is defined with TYPE statements enclosing a DECLARE line for each field, and closed with ENDTYPE:

TYPE EventRecord
    DECLARE EventName : STRING
    DECLARE TicketPrice : REAL
    DECLARE Attending : BOOLEAN
ENDTYPE

[3 marks]: [1] for the TYPE EventRecordENDTYPE structure, [1] for all three fields declared with the correct names (EventName, TicketPrice, Attending), [1] for each field given the correct data type (STRING, REAL, BOOLEAN respectively).

Part (b): Declaring a variable and storing values in its fields

EventRecord is only a data type. A variable of this type must be declared before its fields can be used. Once declared, an individual field of a record variable is accessed using dot notation: <record variable>.<field name>.

DECLARE NextEvent : EventRecord

NextEvent.EventName ← "Chess Club Finals"
NextEvent.TicketPrice ← 2.50
NextEvent.Attending ← TRUE

[2 marks]: [1] for the correct declaration DECLARE NextEvent : EventRecord, [1] for all three dot-notation assignment statements being syntactically correct, with the string value in quotation marks, the real number written as 2.50, and the boolean literal TRUE left unquoted.

Part (c): Reading a field value

To output the value currently stored in the TicketPrice field of NextEvent, the same dot notation is used inside an OUTPUT statement:

OUTPUT NextEvent.TicketPrice

Using the values assigned in part (b), NextEvent.TicketPrice holds 2.50, so this statement outputs 2.50. [1 mark]

Final answers

  • (a) TYPE EventRecord / DECLARE EventName : STRING / DECLARE TicketPrice : REAL / DECLARE Attending : BOOLEAN / ENDTYPE
  • (b) DECLARE NextEvent : EventRecord, with NextEvent.EventName ← "Chess Club Finals", NextEvent.TicketPrice ← 2.50, NextEvent.Attending ← TRUE
  • (c) OUTPUT NextEvent.TicketPrice, which outputs 2.50