Past-paper question · 2018 October-November · Paper 23 · Q5

Question 5 2018 · October-November · Paper 23 · 10 marks

Original past-paper question for 5
Original past-paper question for 5
Original past-paper question for 5
Reveal official mark-scheme answer Official mark-scheme answer for 5

Official answer for Question 5

✓
FUNCTION ReadFileLine(FileName: STRING,

Question 10

✓
FileLine: INTEGER) RETURNS STRING
✓
DECLARE FileData : STRING
✓
DECLARE LineNumber : INTEGER
✓
OPENFILE FileName FOR READ
✓
LineNumber ← 0
✓
// no line read yet
✓
WHILE (NOT EOF(FileName)) AND FileLine <> LineNumber
✓
READFILE FileName, FileData
✓
LineNumber ← LineNumber + 1
✓
ENDWHILE
✓
IF FileLine <> LineNumber
✓
THEN
✓
FileData ← "****"
✓
ENDIF
✓
CLOSEFILE FileName
✓
RETURN FileData
✓
ENDFUNCTION
✓
1 mark for each of the following:

Question 1

✓
Function heading including parameters.

Question 2

✓
Declare local variables FileData and LineNumber

Question 3

✓
Open FileName in READ mode

Question 4

✓
WHILE loop

Question 5

✓
Call to READFILE() (in a loop)

Question 6

✓
Incrementing LineNumber (in a loop)

Question 7

✓
IF FileLine <> LineNumber (after a loop)

Question 8

✓
...Set FileData to "****"

Question 9

✓
Close FileName

Question 10

✓
Return FileData
✓
*** End of Mark Scheme – program code solutions follow ***
✓
9608/23
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2018
✓
Program Code Example Solutions
✓
Q4(b): Visual Basic
✓
FUNCTION Update(ByVal NewData AS STRING) AS INTEGER
✓
DIM ArrayIndex AS INTEGER
✓
DIM Found AS BOOLEAN
✓
DIM Validate AS BOOLEAN
✓
ArrayIndex = 1
✓
Found = FALSE
✓
WHILE ArrayIndex <= LastElement AND Found = FALSE
✓
IF ThisArray[ArrayIndex, 1] > NewData
✓
THEN
✓
Found = TRUE
✓
ELSE
✓
ArrayIndex = ArrayIndex + 1
✓
ENDIF
✓
ENDWHILE
✓
**IF Found = TRUE
✓
THEN
✓
Validate = Insert(ArrayIndex, NewData)
✓
IF Validate = FALSE
✓
THEN
✓
ArrayIndex = -1
✓
ENDIF
✓
ELSE
✓
ArrayIndex = 0
✓
ENDIF
✓
RETURN ArrayIndex // Update = ArrayIndex
✓
ENDFUNCTION
✓
** Alternative
✓
IF Found = FALSE
✓
THEN
✓
ArrayIndex = 0
✓
ELSE
✓
Validate = Insert(ArrayIndex, NewData)
✓
IF Validate = FALSE
✓
THEN
✓
ArrayIndex = -1
✓
ENDIF
✓
ENDIF
✓
RETURN ArrayIndex
✓
Q4(b): Pascal
✓
function Update(NewData: string): integer;
✓
9608/23
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2018
✓
var ArrayIndex : integer;
✓
var Found : boolean;
✓
var Validate : boolean;
✓
begin
✓
ArrayIndex := 1;
✓
Found := FALSE;
✓
while ArrayIndex <= LastElement AND Found = FALSE do
✓
begin
✓
if ThisArray[ArrayIndex, 1] > NewData then found := True
✓
else ArrayIndex := ArrayIndex + 1;
✓
end;
✓
if Found = TRUE then
✓
begin
✓
Validate := Insert(ArrayIndex, NewData);
✓
if Validate = FALSE then
✓
ArrayIndex := -1;
✓
end
✓
else
✓
begin
✓
ArrayIndex := 0;
✓
end;
✓
Update := ArrayIndex;
✓
end;
✓
Q4(b): Python
✓
def Update(NewData):
✓
# ArrayIndex AS INTEGER
✓
# Found AS BOOLEAN
✓
# Validate AS BOOLEAN
✓
ArrayIndex = 1
✓
Found = FALSE
✓
LastElement = 20
✓
while ArrayIndex <= LastElement AND Found == FALSE:
✓
if ThisArray[ArrayIndex][1] > NewData:
✓
Found = TRUE
✓
else:
✓
ArrayIndex = ArrayIndex + 1
✓
if Found == TRUE:
✓
Validate = Insert(ArrayIndex, NewData)
✓
if Validate == FALSE:
✓
ArrayIndex = -1
✓
else:
✓
ArrayIndex = 0
✓
return ArrayIndex
✓
9608/23
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2018
✓
Q4(d)(i): Visual Basic
✓
Dim i AS Integer
✓
For i = 1 to 200
✓
If CharArray(i) >= ‘0’ AND CharArray(i) <= ‘9’ then
✓
CharArray(i) = ‘*’
✓
Endif
✓
Next i
✓
Q4(d)(i): Pascal
✓
var i : integer;
✓
for i := 1 to 200 do
✓
begin
✓
If CharArray(i) >= ‘0’ AND CharArray(i) <= ‘9’ then
✓
CharArray(i):= ‘*’;
✓
end;
✓
Q4(d)(i): Python
✓
#i as string
✓
for i in CharArray:
✓
if CharArray.isdigit:
✓
i = '*'
✓
** Alternative
✓
# i as integer
✓
for i in range(200):
✓
if CharArray[i] >= '0' and CharArray[i] <= '9':
✓
CharArray[i] = '*'

Mark scheme 9608_w18_ms_23.pdf →

Show text version of question

Question 5

The function ReadFileLine() returns a specific line from a text file.

The function takes two parameters:

Identifier Data type Description FileName STRING The name of the text file FileLine INTEGER The line number that is required

The following pseudocode gives an example of the use of the function.

FileData ReadFileLine(FileName, FileLine)

The function ReadFileLine() will:

  • open the file, FileName
  • read each line from the file until line FileLine is found or the end of the file is reached
  • if the line exists, return the string from this line; otherwise return the string "****"

Question 15

Write pseudocode for the ReadFileLine() function.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Question 16

Appendix Built-in functions (pseudocode) Each function returns an error if the function call is not properly formed.

MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING returns a string of length y starting at position x from ThisString

Example: MID("ABCDEFGH", 2, 3) returns string "BCD"

LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING returns leftmost x characters from ThisString

Example: LEFT("ABCDEFGH", 3) returns string "ABC"

RIGHT(ThisString: STRING, x : INTEGER) RETURNS STRING returns rightmost x characters from ThisString

Example: RIGHT("ABCDEFGH", 3) returns string "FGH"

LENGTH(ThisString : STRING) RETURNS INTEGER returns the integer value representing the length of ThisString

Example: LENGTH("Happy Days") returns 10

INT(x : REAL) RETURNS INTEGER returns the integer part of x

Example: INT(27.5415) returns 27

ASC(ThisChar : CHAR) RETURNS INTEGER returns the ASCII value of ThisChar

Example: ASC('A') returns 65

IS_NUM(ThisString : STRING) RETURNS BOOLEAN returns the value TRUE if ThisString contains only numeric characters ('0' to '9').

Example: IS_NUM("1234X67") returns FALSE

Operators (pseudocode)

Operator Description Concatenates (joins) two strings & Example: "Summer" & " " & "Pudding" produces "Summer Pudding" Performs a logical AND on two Boolean values AND Example: TRUE AND FALSE produces FALSE Performs a logical OR on two Boolean values OR Example: TRUE OR FALSE produces TRUE

Topic relevance

Why this question appears here

99% assigned-topic match · Data types and structures › Data types and records

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9608_w18_qp_23.pdf

Keep practising this topic