9618/23/M/J/24 question 4: worked solution (May/June 2024, paper 23)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q29 9618/23/M/J/24 original Q4 Structured · 11 marks Worked solution (a) Use two loops one after the other. Each one stops when a zero is entered. PROCEDURE TwoParts() DECLARE NextNum, Average : REAL TotalA ← 0.0 TotalB ← 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 Procedure heading and ending [1] Local variables declared (NextNum and Average) [1] TotalA and TotalB set to 0 at the start [1] First conditional loop that adds to TotalA until zero is entered [1] Second conditional loop that adds to TotalB until zero is entered [1] Average of the two totals worked out and output with a message [1] (b)(i) Write: a 1D array [1] It holds 20 real values [1] (b)(ii) Write any three of these: Many values are held under one identifier , so fewer variable names are needed [1] The values are easier to process, search or access , for example with a loop and an index [1] The program is easier to write, understand, maintain, test or debug [1] KEY IDEA When the number of similar values grows, use an array so one loop and one name can handle them all. TOTAL 11 marks Official mark scheme VIDEO ▶ Starts at Q4 (27:54) Starts at Q4 (27:54) · by Pseudocode Pro. Tap the picture to open it on YouTube.