Past-paper question · 2021 May-June · Paper 22 · Q6(c)

Question 6(c) 2021 · May-June · Paper 22 · 14 marks

Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Original past-paper question for 6(C)
Reveal official mark-scheme answer Official mark-scheme answer for 6(C)

Official answer for Question 6

Part (c)

✓
'Pseudocode' solution included here for development and clarification of

Question 8

✓
mark scheme.
✓
Programming language example solutions appear in the Appendix.
✓
FUNCTION GetValidFilename() RETURNS STRING
✓
DECLARE Filename : STRING
✓
DECLARE Valid : BOOLEAN
✓
DECLARE Index : INTEGER
✓
DECLARE NextChar : CHAR
✓
REPEAT
✓
Valid ← TRUE
✓
OUTPUT "Please input the name for the backup file "
✓
INPUT Filename
✓
IF LENGTH(Filename) < 4 OR LENGTH(Filename) > 10
✓
THEN
✓
Valid ← FALSE
✓
ELSE
✓
Index ← 1
✓
WHILE Index <= LENGTH(Filename) AND Valid = TRUE
✓
NextChar ← MID(Filename, Index, 1)
✓
IF NOT ((NextChar >='a' AND NextChar <='z')
✓
OR (NextChar >='A' AND NextChar <='Z')
✓
OR (NextChar >='0' AND NextChar <='9'))
✓
THEN
✓
Valid ← FALSE //not alphanumeric
✓
ENDIF
✓
Index ← Index + 1
✓
ENDWHILE
✓
ENDIF
✓
IF Valid = FALSE THEN
✓
OUTPUT "Invalid filename – please try again"
✓
ENDIF
✓
UNTIL Valid = TRUE
✓
RETURN Filename
✓
ENDFUNCTION
✓
One mark for each of the following:
✓
1 Conditional loop until valid filename input

Question 2

✓
Prompt and Input of filename in a loop

Question 3

✓
Test length is within range

Question 4

✓
Loop through filename:

Question 5

✓
Extract a single character

Question 6

✓
Test that character is numeric in a loop

Question 7

✓
Test that character is alphabetic in a loop

Question 8

✓
If filename invalid, output warning and repeat, if valid then return
✓
filename
✓
*** End of Mark Scheme – example program code solutions follow ***
✓
Program Code Example Solutions
✓
9608/22
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2021
✓
PUBLISHED
✓
Question 4(b): Visual Basic
✓
Function Search(Par1, Par2 As STRING) As INTEGER
✓
Dim Index, RetVal As INTEGER
✓
Index = 1
✓
RetVal = -1
✓
While Index <= 100 And RetVal = -1
✓
If XRef(Index, 1) = Par1 _
✓
AND (XRef(Index, 2) = Par2 OR XRef(Index, 3) = Par2) Then
✓
RetVal = Index
✓
End If
✓
Index = Index + 1
✓
End While
✓
Return RetVal
✓
End Function
✓
Question 4(b): Pascal
✓
function Search(Par1, Par2 : string) : integer;
✓
var
✓
Index : integer;
✓
RetVal : integer;
✓
begin
✓
Index := 1;
✓
RetVal := -1;
✓
While Index <= 100 And RetVal = -1 do
✓
begin
✓
if XRef[Index, 1] = Par1
✓
and (XRef[Index, 2] = Par2 or XRef[Index, 3] = Par2) then
✓
RetVal := Index;
✓
Index := Index + 1;
✓
end;
✓
Search := RetVal // result := RetVal
✓
end;
✓
9608/22
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2021
✓
PUBLISHED
✓
Question 4(b): Python
✓
def Search(Par1, Par2):
✓
## Index, RetVal As INTEGER
✓
Index = 1
✓
RetVal = -1
✓
while Index <= 100 and RetVal = -1:
✓
if XRef[Index][1] == Par1 \
✓
and XRef[Index][2] == Par2 or XRef[Index][3] == Par2:
✓
RetVal = Index
✓
Index = Index + 1
✓
return RetVal
✓
Question 5(b)(ii): Visual Basic
✓
Function Verify(UserID As String) As Boolean
✓
Dim Count As INTEGER
✓
Dim Response As BOOLEAN
✓
Dim Password As STRING
✓
Count = 1
✓
Response = FALSE
✓
If UserID = "Guest" Then
✓
Return TRUE
✓
End If
✓
While Response = FALSE And Count < 4
✓
Console.Writeline("Please input your password: ")
✓
Password = Console.Readline()
✓
Response = Validate(UserID, Password) AND TODAY()
✓
If Response = FALSE Then
✓
Count = Count + 1
✓
If Count < 4 Then
✓
Console.Writeline("Incorrect password – please try again")
✓
End If
✓
End If
✓
End While
✓
Return Response
✓
End Function
✓
9608/22
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2021
✓
PUBLISHED
✓
Question 5(b)(ii): Pascal
✓
function Verify(UserID : string) : boolean;
✓
var
✓
Count : integer;
✓
Response : boolean;
✓
Password : string;
✓
begin
✓
Count := 1;
✓
if UserID = "Guest" then
✓
Verify := TRUE // result := TRUE;
✓
while Response = FALSE And Count < 4
✓
begin
✓
writeln('Please input your password: ');
✓
readln(Password);
✓
Response := Validate(UserID, Password) and TODAY();
✓
if Response = FALSE then
✓
begin
✓
Count := Count + 1;
✓
if Count < 4 then
✓
writeln('Incorrect password – please try again');
✓
end;
✓
end;
✓
Verify := Response // result :=
✓
Response
✓
end;
✓
Question 5(b)(ii): Python
✓
def Verify(UserID):
✓
## Count As INTEGER
✓
## Response As BOOLEAN
✓
## Password As STRING
✓
Count = 1
✓
if UserID == "Guest":
✓
return TRUE
✓
while Response == FALSE and Count < 4:
✓
Password = input("Please input your password: ")
✓
Response = Validate(UserID, Password) and TODAY()
✓
if Response == FALSE:
✓
Count = Count + 1
✓
if Count < 4:
✓
print("Incorrect password – please try again")
✓
return Response

Mark scheme 9608_s21_ms_22.pdf →

Show text version of question

Question 6

Part (c)

Question 6

A program stores data about stock items in four global 1D arrays as follows:

Example Initial data Array Data type Description data value value the stock item ID StockID STRING "JBCD0002" "" (eight alpha-numeric characters) a description of the item Description STRING "soap" "" (alphabetic characters only) Quantity INTEGER the number in stock 9 0 Cost REAL the cost of the item 1.45 0.0

  • Each array contains 10 000 elements.
  • Elements with the same index relate to the same stock item. For example, StockID[3]
  • contains the ID for the product whose description is in Description[3].

  • The StockID array is not sorted.

The program will be modified so that the data from the arrays can be stored in a text file for backup. You may assume that a backup file contains only valid stock data.

The programmer has started to define program modules as follows:

Module Description

  • called with two parameters:
  • an array index
  • Unpack()

  • a string value read from one line of the backup file
  • extracts the four data values from the string and assigns each
  • to the appropriate array

  • called with a string representing the name of a backup file
  • returns FALSE if the file is empty
  • sets all elements of each array to the initial data value as given
  • in the table Restore() • reads the backup file line by line calls Unpack() to extract data from each line and assign values to the corresponding arrays

  • returns FALSE if the arrays are full but there are still lines in the
  • file, otherwise returns TRUE For all items where StockID does not contain the initial value:

  • counts the number of stock entries in the StockID array
  • StockSummary() • outputs the overall value of all items in stock (cost multiplied by the quantity)

  • outputs the number of stock entries

Question 13

Part (c)

The module description of GetValidFilename() is as follows:

Module Description

  • prompts and inputs a filename
  • validates the filename by checking that it:
  • is between 4 and 10 characters in length (inclusive)
  • GetValidFilename() • contains only alphanumeric characters

  • if the filename is invalid, outputs a warning message and
  • asks the user to try again

  • otherwise returns the valid filename

Write program code for the module GetValidFilename().

Visual Basic and Pascal: You should include the declaration statements for variables. Python: You should show a comment statement for each variable used with its data type.

Programming language .............................................................................................................

Program code

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Question 17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[8]

Question 18

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 "BCD"

LENGTH(ThisString : STRING) RETURNS INTEGER returns the integer value representing the length of 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 "ABC"

RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING returns rightmost x characters from ThisString Example: RIGHT("ABCDEFGH", 3) returns "FGH"

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

LCASE(ThisChar : CHAR) RETURNS CHAR returns the character value representing the lower case equivalent of ThisChar If ThisChar is not an upper case alphabetic character, it is returned unchanged. Example: LCASE('W') returns 'w'

ASC(ThisChar : CHAR) RETURNS INTEGER returns the ASCII value of character ThisChar Example: ASC('A') returns 65

CHR(x : INTEGER) RETURNS CHAR returns the character whose ASCII value is x Example: CHR(87) returns 'W'

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 19

Question 20

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 Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cambridgeinternational.org after the live examination series.

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

Topic relevance

Why this question appears here

91% 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_s21_qp_22.pdf

Keep practising this topic