Cambridge AS Level Computer Science 9618 · 10.3 Files

Q58 · 9618/22/M/J/24 Q6

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.

Q589618/22/M/J/24original Q6Structured · 10 marks
Question 6 from 9618/22/M/J/24

Worked solution

(a)

If the title has 16 characters or fewer, return it unchanged. Otherwise remove words from the end until the title is shorter than 14 characters, then add three dots.

FUNCTION Trim(Name : STRING) RETURNS STRING
    CONSTANT Dots = "..."
    CONSTANT Space = " "
    IF LENGTH(Name) <= 16 THEN
        RETURN Name
    ENDIF
    WHILE LENGTH(Name) > 13
        REPEAT
            Name ← LEFT(Name, LENGTH(Name) - 1)
        UNTIL RIGHT(Name, 1) = Space
    ENDWHILE
    Name ← LEFT(Name, LENGTH(Name) - 1)
    Name ← Name & Dots
    RETURN Name
ENDFUNCTION

Function heading, parameter, return type and ending [1]

If the length is 16 or less, the original title is returned [1]

A conditional loop that repeats until the string is short enough (less than 14) [1]

An inner loop (or second condition) that finds the word to remove from the end [1]

Characters are stripped back to a space [1]

The space in front of the removed word is also removed [1]

The three dots are joined on and the result is returned [1]

(b)(i)

Write: the file becomes very large, because redundant zeros are stored [1]

(b)(ii)

Write one of these: put a separator (delimiter) character between the values, or make the first character show the length of each sample [1]

(b)(iii)

Write: the algorithm to store and extract the samples is more complex and slower to run [1]

KEY IDEATrim a string by removing words from the end until it fits, and remember to remove the space too.
TOTAL 10 marks

Official mark scheme

Mark scheme for 9618/22/M/J/24 question 6
VIDEO
▶ Whole paper

Video solution of the whole paper · by Pseudocode Pro. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.