Advanced Data Representation and File Organisation: Question 7

Syllabus 13.1

Structured A2 7 marks

A small bookshop wants to store, for each book on one shelf, four pieces of data: its Title, its Author, its Price, and whether it is currently InStock.

(a) Write a pseudocode TYPE definition, BookRecord, that declares these four fields with appropriate data types. [2]

(b) Write a pseudocode declaration for Bookshelf, an array able to hold BookRecord values for exactly 3 books. [1]

(c) Write pseudocode statements that set the second book's Title, Author, Price and InStock fields in Bookshelf to "The Hobbit", "Tolkien", 8.50 and TRUE, respectively. [2]

(d) Write a pseudocode FOR loop that outputs the Title field of every book currently held in Bookshelf. [2]

Show worked solution Hide worked solution

Worked solution

Part (a): Defining the BookRecord type

A composite user-defined data type that groups several named fields together is defined with TYPE ... ENDTYPE, listing each field’s name and data type with DECLARE inside:

TYPE BookRecord
    DECLARE Title : STRING
    DECLARE Author : STRING
    DECLARE Price : REAL
    DECLARE InStock : BOOLEAN
ENDTYPE

Title and Author are text, so STRING is correct for both. Price needs a decimal value (such as 8.50), so it must be REAL, not INTEGER. InStock only ever needs to be true or false, so BOOLEAN is correct.

[2 marks]: [1] for the TYPE ... ENDTYPE structure with all four fields declared, [1] for every field given the correct data type (STRING, STRING, REAL, BOOLEAN).

Part (b): Declaring the Bookshelf array

Once BookRecord exists as a type, an array can be declared to hold several BookRecord values, exactly as an array of any other type would be declared:

DECLARE Bookshelf : ARRAY[1:3] OF BookRecord

This creates three elements, Bookshelf[1] to Bookshelf[3], each one able to hold a complete set of the four BookRecord fields.

[1 mark] for the correct declaration, ARRAY[1:3] OF BookRecord.

Part (c): Setting the second book’s fields

A specific field of a specific array element is reached by first indexing the array, then using . (dot notation) to reach the field within that element’s record:

Bookshelf[2].Title ← "The Hobbit"
Bookshelf[2].Author ← "Tolkien"
Bookshelf[2].Price ← 8.50
Bookshelf[2].InStock ← TRUE

[2 marks]: [1] for the correct dot-notation form Bookshelf[2].<Field>, [1] for all four fields assigned the correct stated values.

Part (d): Outputting every book’s Title

Bookshelf is indexed from 1 to 3 inclusive (it was declared ARRAY[1:3] OF BookRecord), so a FOR loop over i ← 1 TO 3 visits every element exactly once:

FOR i ← 1 TO 3
    OUTPUT Bookshelf[i].Title
NEXT i

Each iteration accesses the Title field of the current array element, Bookshelf[i], using the same dot notation as part (c).

[2 marks]: [1] for the loop bounds 1 TO 3 matching the array’s declared indices, [1] for correctly outputting Bookshelf[i].Title inside the loop.

Final answers

  • (a) TYPE BookRecord with fields Title : STRING, Author : STRING, Price : REAL, InStock : BOOLEAN, closed with ENDTYPE.
  • (b) DECLARE Bookshelf : ARRAY[1:3] OF BookRecord
  • (c) Bookshelf[2].Title ← "The Hobbit", Bookshelf[2].Author ← "Tolkien", Bookshelf[2].Price ← 8.50, Bookshelf[2].InStock ← TRUE
  • (d) FOR i ← 1 TO 3 / OUTPUT Bookshelf[i].Title / NEXT i