Skip to content

11.2 Constructs

AS Level · 8 questions found

  • IF…ELSE and nested IF statements; CASE structure
  • Count-controlled loop (FOR), pre-condition (WHILE), post-condition (REPEAT)
  • Justify which loop structure best suits a given problem
Q2
May/Jun 2024 Paper 2 v1 5 marks
Question 2 — page 1Question 2 — page 2Question 2 — page 3
2 An algorithm has three steps. It will: 1. repeatedly input a pair of numeric values A and B 2. count the number of pairs that are input until A has been greater than B 10 times 3. output the number of pairs that were input. (a) Complete the program flowchart. START END INPUT A, B Yes No [5] (b) Step 1 of the algorithm is changed. A variable ThisSequence is used to enter a sequence of 10 pairs of numeric values, using a single input statement. Following the input of ThisSequence the revised algorithm will extract the pairs of numbers. Describe the variable ThisSequence and how the numbers are extracted. ............................................................................................................................................. [2]
Show mark scheme
2(a) [2 marks]
One mark per outlined region:
1
Initialise both counts
2
Increment
every time a pair is input
Tries
3
Compare A > B
and
increment
if TRUE
Count
4
Test for Count = 10 (10
time A > B) – MUST include Yes / No labels
th
5
If so output
, otherwise loop
Tries
2(b) [3 marks]
One mark per point:
1
A (variable of type) string will be input // by example e.g. “67,72”
2
A special / identified character would need to be used to separate each
numeric value // all numbers are fixed length
Q4
May/Jun 2024 Paper 2 v1
Question 4 — page 1Question 4 — page 2Question 4 — page 3
4 A function Check() will: • total the element values in odd index locations (1, 3, 5 ... 97, 99) • total the element values in even index locations (2, 4, 6 ... 98, 100) • return one of three strings ‘Odd’, ‘Even’ or ‘Same’ to indicate which total is the greater, or whether the totals are the same. Write pseudocode for the function Check(). .................................................................................................................................................... [6] BLANK PAGE
Show mark scheme
4
Example:
FUNCTION Check() RETURNS STRING
DECLARE Odd, Even, Index : INTEGER

Odd
0

Even
0

FOR Index
1 TO 100
IF Index MOD 2 = 0 THEN

Even
Even + Data[Index]
ELSE

Odd
Odd + Data[Index]
ENDIF
NEXT Index
ENDFUNCTION
Mark as follows:
1. Function heading, ending and return type
2. Declare local variables
and
as integers
Odd, Even
Index
3. Initialise
and
Odd
Even
4. Loop for 100
// through array
iterations
5. Sum
and
element values
in a loop
Odd
Even
6. Compare
and
after the loop and Return appropriate string
Odd
Even
Q7
May/Jun 2024 Paper 2 v1 17 marks
Question 7 — page 1Question 7 — page 2Question 7 — page 3Question 7 — page 4Question 7 — page 5Question 7 — page 6Question 7 — page 7Question 7 — page 8Question 7 — page 9Question 7 — page 10Question 7 — page 11Question 7 — page 12Question 7 — page 13
x 7 6 5 4 1 0 2 3 4 5 6 7 8 9 10 A C B 3 2 1 A triangle is said to be right-angled if the following test is true (where A is the length of the longest side): A2 = B2 + C2 A2 means A multiplied by A, for example 32 means 3 × 3 which evaluates to 9 You can calculate A2, B2 and C2 by using the coordinates of the endpoints of each line. For example, B2 is calculated as follows: 10 y x 9 8 7 6 5 4 1 0 2 3 4 5 6 7 8 9 10 B P2 P2 P1 P1 3 2 1 (x1, y1) (x1, y1) (x2, y2) (x2, y2) The endpoints, P1 and P2, have the coordinates (3, 2) and (6, 6). The value B2 is given by the formula: B2 = (x1 − x2)2 + (y1 − y2)2 In this example: B2 = (3 − 6)2 + (2 − 6)2 B2 = (–3)2 + (–4)2 B2 = 9 + 16 B2 = 25 (a) A function IsRA() will: • take three sets of integers as parameters representing the coordinates of the three endpoints that form a triangle • return TRUE if the endpoints form a right-angled triangle, otherwise return FALSE. In pseudocode, the operator ‘^’ represents an exponent, which is the number of times a value is multiplied by itself. For example, the expression Value2 may be written in pseudocode as Value ^ 2 Complete the pseudocode for the function IsRA(). FUNCTION IsRA(x1, y1, x2, y2, x3, y3 : INTEGER) RETURNS BOOLEAN ENDFUNCTION [6] (b) The test used to check if a triangle is right-angled can be written in two ways: A2 = B2 + C2 or A = √(B2 + C2) The symbol √ represents the square root operation. For example, √81 = 9 A new function SQRT() is written to perform the square root operation. The function takes an integer number as a parameter and returns a positive real value representing the square root of the number. During testing it is found that the SQRT() function returns a value that is only accurate to 4 decimal places. For example, SQRT(25) returns 5.0000125 rather than the correct value of 5.0 The function IsRA() from part (a) is modified to use the new SQRT() function to test if a triangle is right-angled. Describe a problem that might occur when using the modified IsRA() function and suggest a solution that still allows the SQRT() function to be used. Problem .................................................................................................................................... Solution ..................................................................................................................................... [2] BLANK PAGE 7 A fitness club has a computerised membership system. The fitness club offers a number of different exercise classes. The following information is stored for each club member: name, home address, email address, mobile phone number, date of birth and the exercise(s) they are interested in. (a) When an exercise class is planned, a new module will send personalised text messages to each member who has expressed an interest in that exercise. Members wishing to join the class send a text message back. Members may decide not to receive future text messages by replying with the message ‘STOP’. The process of abstraction is used to filter out unnecessary information. (i) State one advantage of applying abstraction to this problem. ..................................................................................................................................... [1] (ii) Identify three items of information that will be required by the new module. Justify your choices with reference to the given scenario. Item 1 required .................................................................................................................. Justification ....................................................................................................................... Item 2 required .................................................................................................................. Justification ....................................................................................................................... Item 3 required .................................................................................................................. Justification ....................................................................................................................... [3] (iii) Identify two operations that would be required to process data when the new module receives a text message back from a member. Operation 1 ....................................................................................................................... Operation 2 ....................................................................................................................... [2] (b) The structure chart illustrates part of the membership program: Sub-A Sub-B P1 T1 P2 Name Update Sub-C A Data item notes: • Name contains the name of a club member • P1 and T1 are of type real. (i) Explain the meaning of the diamond symbol (labelled with the letter A) in the chart. ..................................................................................................................................... [2] (ii) Write the pseudocode module headers for Sub-A and Sub-B. Sub-A Sub-B [4] 8 A teacher is designing a program to process pseudocode projects written by her students. Each student project is stored in a text file. The process is split into a number of stages. Each stage performs a different task and creates a new file named as shown: File name Comment MichaelAday_src.txt student project file produced by student Michael Aday MichaelAday_S1.txt file produced by stage 1 MichaelAday_S2.txt file produced by stage 2 The teacher has defined the first program module as follows: Module Description DeleteComment() • called with a parameter of type string representing a line of pseudocode from a student’s project file • returns the line after removing any comments Note on comments: A comment starts with two forward slash characters and includes all the remaining characters on the line. The following example shows a string before and after the comment has been removed: Before: IF X2 > 13 THEN //check if limit exceeded After: IF X2 > 13 THEN (a) Complete the pseudocode for module DeleteComment(). FUNCTION DeleteComment(Line : STRING) RETURNS STRING ENDFUNCTION [8] (b) A second module is defined: Module Description Stage_1() • called with a parameter of type string representing a student name • creates a new stage 1 file • copies each line from the student’s project file to the stage 1 file after removing any comment from each line • does not write blank lines to the stage 1 file • returns the number of lines written to the stage 1 file Write pseudocode for module Stage_1(). Module DeleteComment() must be used in your solution. ............................................................................................................................................. [7] BLANK PAGE BLANK PAGE Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every 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 Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge BLANK PAGE
Show mark scheme
7(a)(i) [3 marks]
To make the solution easier to design / implement / solve
7(a)(ii)
One mark per item
and
justification
Item: mobile phone number
Justification: to send the text message
Item: name
Justification: to personalise the text message
Item: exercise interest
Justification: to determine whether this member would be interested
7(a)(iii) [2 marks]
Examples include:

Add a member to a list of those interested in the new class

Remove the member from future SMS messages

Read/process Message

Identify who from
One mark for each.
Max 2 marks
7(b)(i) [4 marks]
Means that
calls (one of) either
or
Update
Sub-A, Sub-B
Sub-C
One mark for each point:

reference to selection / decision / if

naming all four modules correctly
PROCEDURE Sub-A (Name : STRING, BYREF P2 : BOOLEAN)
7(b)(ii) [8 marks]
FUNCTION Sub-B (P1 : REAL) RETURNS REAL
One mark per underlined part in each case
FUNCTION DeleteComment(Line : STRING) RETURNS STRING
Q9
May/Jun 2024 Paper 2 v1
Question 9
9
Q1
May/Jun 2024 Paper 2 v2 9 marks
Question 1 — page 1Question 1 — page 2Question 1 — page 3
1 (a) The following table contains pseudocode examples. Each example may contain statements that relate to one or more of the following: • selection • iteration (repetition) • input/output. Complete the table by placing one or more ticks (3) in each row. Pseudocode example Selection Iteration Input/Output FOR Index 1 TO 10 Data[Index] 0 NEXT Index WRITEFILE ThisFile, "****" UNTIL Level > 25 IF Mark > 74 THEN READFILE OldFile, Data ENDIF [4] (b) Program variables have data types as follows: Variable Data type MyChar CHAR MyString STRING MyInt INTEGER Complete the table by filling in each gap with a function (from the insert) so that each expression is valid. Expression MyInt .......................................... (3.1415926) MyChar .......................................... ("Elwood", 3, 1) MyString .......................................... ( .......................................... (27.509)) MyInt .......................................... ( .......................................... ("ABC123", 3)) [4] (c) The variables given in part (b) are chosen during the design stage of the program development life cycle. The choices are to be documented to simplify program maintenance. State a suitable way of documenting the variables and give one piece of information that should be recorded, in addition to the data type. ............................................................................................................................................. [2]
Show mark scheme
1(a) [4 marks]
Pseudocode example
Selection
Iteration
Input/Output

FOR Index
1 TO 10


Data[Index]
0
NEXT Index
WRITEFILE ThisFile,

"****"
UNTIL Level > 25

IF Mark > 74 THEN
READFILE OldFile,


Data
ENDIF
One mark per row.
1(b)
Expression

INT
MyInt
(3.1415926)

MID
MyChar
("Elwood", 3,
1)
Any of:


MyString
NUM_TO_STR
(
INT
(27.509))


MyString
CHR
(
INT
(27.509))


MyString
TO_UPPER
(
NUM_TO_STR(
27.509))


MyString
TO_LOWER
(
NUM_TO_STR
( 27.509))
Any of:


STR_TO_NUM ( RIGHT
MyInt
("ABC123", 3))


LENGTH ( RIGHT
MyInt
("ABC123", 3))


LENGTH ( LEFT
MyInt
("ABC123", 3))
One mark per row
1(c) [5 marks]
1 mark for stating a suitable way of documenting:

Identifier table
1 mark for giving one piece of information that should be recorded.
examples include:
Explanation of what (each) variable is used for
The purpose of (each) variable
An example of data values stored // Initialisation value
Q2
May/Jun 2024 Paper 2 v2 9 marks
Question 2 — page 1Question 2 — page 2Question 2 — page 3Question 2 — page 4
2 A program is being developed. (a) An algorithm for part of the program will: • input three numeric values and assign them to identifiers Num1, Num2 and Num3 • assign the largest value to variable Ans • output a message giving the largest value and the average of the three numeric values. Assume the values are all different and are input in no particular order. Complete the program flowchart on page 5 to represent the algorithm. START END INPUT Numl, Num2, Num3 Yes No [5] (b) A different part of the program contains an algorithm represented by the following program flowchart: START END Set Flag to GetStat() Set Port to 1 Is Flag = TRUE ? Is Port = 4 ? Yes No No Yes CALL Reset(Port) Set Port to Port + 1 Write pseudocode for the algorithm. ............................................................................................................................................. [5]
Show mark scheme
2(a) [5 marks]
Mark points
1
Condition for selecting one of the input numbers as largest value
2
… and assign to
Ans
3
Condition for selecting largest number for all three number input and
assigning to
Ans
4
Average calculated using
,
and
and stored in a variable.
Num1
Num2
Num3
Reject use of DIV
5
Output of
and average in output symbol / parallelogram
Ans
2(b) [4 marks]
Example solutions:

Flag
GetStat()
WHILE Flag <> TRUE

FOR Port
1 TO 3
CALL Reset(Port)
NEXT Port

Flag
GetStat()
ENDWHILE
Alternative:
REPEAT

Flag
GetStat()
IF Flag <> TRUE THEN

FOR Port
1 TO 3
CALL Reset(Port)
NEXT Port
ENDIF
UNTIL Flag = TRUE
One mark per point:
1
(Outer) conditional loop testing
Flag
2
Correct assignment of
from
in a loop
Flag
GetStat()
3
(Inner) loop checking / counting port // Check if
is different to 4
Port
4
… loop for 3 iterations
5
a call to
in a loop
Reset()
Q4
May/Jun 2024 Paper 2 v2
Question 4 — page 1Question 4 — page 2Question 4 — page 3
4 A triangle has sides of length A, B and C. C A B In this example, A is the length of the longest side. This triangle is said to be right‑angled if the following equation is true: A × A = (B × B) + (C × C) A procedure will be written to check whether three lengths represent a right‑angled triangle. The lengths will be input in any sequence. The procedure IsRA() will: • prompt and input three integer values representing the three lengths • test whether the three lengths correspond to the sides of a right‑angled triangle • output a suitable message. The length of the longest side may not be the first value input. Write pseudocode for the procedure IsRA(). .................................................................................................................................................... [5]
Show mark scheme
4
Example solution:
PROCEDURE IsRA()
DECLARE a, b, c : INTEGER
OUTPUT "Input length of the first side"
INPUT a
OUTPUT "Input length of the second side"
INPUT b
OUTPUT "Input length of the third side"
INPUT c
IF (a * a = (b * b) + (c * c)) OR__
(b * b = (a * a) + (c * c)) OR__
(c * c = (a * a) + (b * b)) THEN
OUTPUT "It is right-angled"
ELSE
OUTPUT "Not right-angled"
ENDIF
ENDPROCEDURE
Mark as follows:
1. Procedure heading and ending
and
declaration of all variables used
2. Appropriate prompt and input for each length
3.
One
correct length test
4.
All three
length tests // selection of which test is required
5. Output one of two messages following a reasonable attempt at MP3
Q4
May/Jun 2024 Paper 2 v3 5 marks
Question 4 — page 1Question 4 — page 2Question 4 — page 3
4 A procedure TwoParts() will input a sequence of real values, one at a time. The procedure will: • process the sequence in two parts • form a first total by adding the values until the first zero • form a second total by adding the values after the first zero until the second zero • output the average of the two totals, together with a suitable message. Values input in the first part are totalled using global variable TotalA and those input in the second part are totalled using global variable TotalB. (a) Write pseudocode for the procedure TwoParts(). ............................................................................................................................................. [6] (b) The value zero denotes the split between the two parts of the sequence. The requirement changes and now there may be up to 20 parts. (i) Identify a suitable data structure that could be used to store the different total values. ..................................................................................................................................... [2] (ii) Describe three benefits of using the data structure given in part (b)(i). 1 ........................................................................................................................................ 2 ........................................................................................................................................ 3 ........................................................................................................................................ [3] ,   , ,  ,
Show mark scheme
4(a) [2 marks]
Example solution:
PROCEDURE TwoParts()
DECLARE NextNum, Average : REAL

TotalA
0.0 // 0

TotalB
0.0 // 0
REPEAT
INPUT NextNum

TotalA
TotalA + NextNum
UNTIL NextNum = 0
REPEAT
INPUT NextNum

TotalB
TotalB + NextNum
UNTIL NextNum = 0

Average
(TotalA + TotalB) / 2
OUTPUT "The average is ", Average
ENDPROCEDURE
Mark as follows:
1. Procedure heading and ending
2. Declare all local variables
3. Initialise
and
TotalA
TotalB
4. First conditional loop until zero entered, summing
// Loop until
TotalA
both parts (sequences) have been entered
5. Second conditional loop until zero entered, summing
// Loop
TotalB
summing appropriate Totals
6. Calculation of average and output with a message // Calculation of the
average for the values making up the two totals and both output with a
suitable message
4(b)(i) [3 marks]
(1D) array of 20 reals
Marks as follows:
1 mark for array
1 mark for 20 reals
4(b)(ii)
One mark per point:
1
(Multiple instances referenced via a single identifier so)
fewer identifiers
needed
2
Easier to process / search / organise / access the data // Values may be
accessed via a loop-controlled variable /an index //An array / data can be
iterated through
3
Makes the program/algorithm easier to write / design / understand /
maintain / modify // Simplifies the program // Easier to debug / test