Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Open the file to write, get data with GetData() until it is "**", write each piece, add its length to a count, then output the total.
PROCEDURE ReceiveFile(FileName : STRING) DECLARE FileData : STRING DECLARE CharCount : INTEGER CONSTANT Terminator = "**" OPENFILE FileName FOR WRITE FileData ← GetData() CharCount ← 0 WHILE FileData <> Terminator WRITEFILE FileName, FileData CharCount ← CharCount + LENGTH(FileData) FileData ← GetData() ENDWHILE CLOSEFILE FileName OUTPUT CharCount, " characters were written to ", FileName ENDPROCEDURE
File opened in WRITE mode and closed afterwards [1]
Conditional loop that runs until the terminator is received [1]
An attempt to use GetData() [1]
GetData() used fully correctly to return the data inside the loop [1]
CharCount kept up to date inside the loop [1]
Each line written to the file, but not the terminator [1]
Final message giving the number of characters, output after the loop [1]
Problem: the file being sent might itself contain a line "**" [1]
The receiving program then ends the file at that point, so later lines are lost [1]
Solution: the sender first sends a message giving the number of lines in the file (or chooses a terminator that is not in the file and sends it before the transfer starts) [1]
Repeat: get data and output it, stop if it is "Bye"; otherwise input a reply, send it as a full message, and stop if the reply is "Bye".
PROCEDURE Chat(Destination : STRING, Port : INTEGER) DECLARE Data : STRING DECLARE Finished : BOOLEAN CONSTANT Terminator = "Bye" CONSTANT STX = CHR(2) CONSTANT ETX = CHR(3) Finished ← FALSE REPEAT Data ← GetData() OUTPUT Data IF Data = Terminator THEN Finished ← TRUE ENDIF IF NOT Finished THEN INPUT Data CALL Transmit(STX & Destination & MyID & Data & ETX, Port) IF Data = Terminator THEN Finished ← TRUE ENDIF ENDIF UNTIL Finished = TRUE ENDPROCEDURE
A conditional loop [1]
Test for "Bye" for both the received and the sent data [1]
GetData() used to get the received data [1]
The received data output inside the loop [1]
The reply input by the user [1]
An attempt to use Transmit() to send the reply inside the loop [1]
Parameters to Transmit() formed correctly (STX, Destination, MyID, data, ETX, and Port) [1]
Limitation: GetData() does not return until a message has arrived [1]
So after sending a message the user must wait for a reply, the chat can only go one way at a time [1]
Modification: let GetData() return straight away with a suitable message (for example after a time limit) if nothing has arrived, which Chat() can detect so the conversation carries on [1]
▶ Starts at Q8 (1:55:35)
Starts at Q8 (1:55:35) · by Zafar Ali Khan (ZAK). 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.