Past-paper question · 2017 October-November · Paper 22 · Q5

Question 5 2017 · October-November · Paper 22 · 9 marks

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

Official answer for Question 5

✓
PROCEDURE RemoveDetails
✓
DECLARE FileLine: STRING
✓
DECLARE MemberToDelete: STRING
✓
OPENFILE "EmailDetails.txt" FOR READ
✓
OPENFILE "NewEmailDetails.txt" FOR WRITE
✓
INPUT MembershipNumber
✓
WHILE NOT EOF("EmailDetails.txt")
✓
READFILE "EmailDetails.txt", FileLine
✓
IF LEFT(FileLine, 4) <> MembershipNumber
✓
THEN
✓
WRITEFILE "NewEmailDetails.txt", FileLine
✓
ENDIF
✓
ENDWHILE
✓
CLOSEFILE "EmailDetails.txt"
✓
CLOSEFILE "NewEmailDetails.txt"
✓
ENDPROCEDURE
✓
Mark as follows:
✓
1. Procedure declaration and end. No parameters.
✓
2.
✓
Variable declaration of STRING for variable FileLine (or similar)
✓
3.
✓
Input the MembershipNumber of the person who has left
✓
4.
✓
Open EmailDetails for READ
✓
5.
✓
Open NewEmailDetails for WRITE
✓
6.
✓
Correct loop checking for EOF(EmailDetails)
✓
7.
✓
Reading a line from EmailDetails.txt in a loop
✓
8.
✓
Correct check for MemberToDelete in a loop
✓
9.
✓
Writing a line to NewEmailDetails.txt in a loop
✓
10. Closing both files (not in a loop)
✓
9608/22
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2017
✓
Appendix - Program Code Example Solutions
✓
Q4 (a): Visual Basic
✓
Dim random As New Random()
✓
Sub TestRandom(ByVal repetitions As Integer)
✓
Dim randinrange As Integer
✓
Dim i As Integer
✓
Dim num(1 To 10) As Integer
✓
Dim freq As Integer
✓
Dim difference As Integer
✓
freq = repetitions / 10
✓
'calculate expected frequency
✓
For i = 1 To 10
✓
'initialise array to store total frequencies
✓
num(i) = 0
✓
Next i
✓
For i = 1 To repetitions 'generate random numbers & increment
✓
appropriate freq
✓
randinrange = random.Next(1, 11)
✓
num(randinrange) = num(randinrange) + 1
✓
Next i
✓
Console.WriteLine("The expected frequency is " & freq)
✓
'report header
✓
Console.WriteLine("Number
✓
Frequency
✓
Difference")
✓
'column headers
✓
For i = 1 To 10
✓
'calc & display difference between expected and actual
✓
freq
✓
difference = num(i) - freq
✓
Console.WriteLine(i & "
✓
" & num(i) & "
✓
" & difference)
✓
Next i
✓
End Sub
✓
Other possible ways of calculating a random number in VB include:
✓
randinrange = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) +
✓
lowerbound
✓
randinrange = math.round((Rnd()*9)+1)
✓
randinrange = CInt(Math.Ceiling(Rnd() * 9
✓
9608/22
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2017
✓
Q4 (a): Pascal
✓
procedure TestRandom(var Repetitions : integer);
✓
var
✓
Frequency : array[1..10] : integer;
✓
Expected, NextRandom, N : integer;
✓
begin
✓
Expected := Round(Repetitions/10);
✓
for N := 1 to 10 do
✓
Frequency[N] := 0;
✓
for N := 1 to Repetitions do
✓
begin
✓
NextRandom := random(10)+1;
✓
Frequency[NextRandom] := Frequency[NextRandom]+1;
✓
end;
✓
writeln ('The expected frequency is ', Expected);
✓
writeln ('Number Frequency Difference');
✓
for N := 1 to 10 do
✓
writeln ('
✓
',N,'
✓
',Frequency[N],'
✓
',Frequency[N]-
✓
Expected);
✓
end;
✓
9608/22
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2017
✓
Q4 (a): Python
✓
# frequency is an array from 1 to 10 of type integer;
✓
# nextNumber is an integer which stores the created random number
✓
# expected is an integer which stores the expected frequency of each number
✓
def TestRandom (repetitions):
✓
import random
✓
frequency = [0 for i in range(1,11)]
✓
# initialise each frequency count
✓
to 0
✓
expected = repetitions / 10
✓
for i in range(1, repetitions + 1):
✓
nextNumber = random.randint(1,10)
✓
frequency[nextNumber] = frequency[nextNumber]+ 1
✓
print ("The expected frequency is ", expected)
✓
print("
✓
Number Frequency Difference")
✓
for i in range(1,11):
✓
print ("
✓
", i, "
✓
", frequency[i],"
✓
", frequency[i] -
✓
expected)
✓
Alternative:
✓
def TestRandom (repetitions):
✓
expected = repetitions / 10
✓
## initialised as real/integer
✓
## NextRandom and N defined as integers
✓
frequency =[0,0,0,0,0,0,0,0,0,0,0] ## defined as an array and
✓
initialised to zero
✓
for n in range (0,repetitions):
✓
nextNumber = randint(1, 10)
✓
frequency[nextNumber] += 1
✓
print ('The expected frequency is ', expected)
✓
print ('Number Frequency Difference')
✓
for n in range (1, 11):
✓
print (n,'
✓
',frequency[n],'
✓
',frequency[n] - expected)
✓
Alternative:
✓
frequency =[0]*11
✓
## alternate way to initialise array to zero
✓
frequency =[]
✓
## empty array/list
✓
Alternative:
✓
for n in range (1,11):
✓
frequency[n-1] = 0
✓
##alternate way to initialise array to zero

Mark scheme 9608_w17_ms_22.pdf →

Show text version of question

Question 5

A
sports club maintains a record of the email address of each of its members. The details are

stored in a text file, EmailDetails.txt. The format of each line of the text file is as follows:

<MembershipNumber><EmailAddress>

  • MembershipNumber is a four-character string of numerals
  • EmailAddress is a variable-length string

When members leave the club their details need to be removed from the file.

A
procedure, RemoveDetails is required. This will perform the following actions:

1. Input the MembershipNumber of the club member to be removed 2. Create a new file, NewEmailDetails.txt 3. Copy all the lines from EmailDetails.txt to NewEmailDetails.txt, except the line with the matching MembershipNumber

Write pseudocode for the procedure, RemoveDetails.

For the built-in functions list, refer to the Appendix on page 15.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[9]

Question 14

Question 15

Appendix

Built-in functions (pseudocode)

In each function, if the function call is not properly formed, the function returns an error.

MODULUS(x : INTEGER, y : INTEGER) RETURNS INTEGER

returns the remainder when x is divided by y using integer arithmetic. Example: MODULUS(5, 2) will return 1

INT(x : REAL) RETURNS INTEGER

returns the integer part of x. Example: INT(27.5415) returns 27

LENGTH(ThisString : STRING) RETURNS INTEGER

returns the integer value representing the length of string ThisString. Example: LENGTH("Happy Days") returns 10

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"

TONUM(ThisString : STRING) RETURNS INTEGER

returns a numeric value equivalent to ThisString. Example: TONUM("1201") returns integer value 1201

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

Question 16

reasonable effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

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_w17_qp_22.pdf

Keep practising this topic