2210/23/O/N/24 question 12: worked solution (October/November 2024, paper 23)
Show as plain textHide plain text
Cambridge O Level Computer Science 2210 · 8.2 Arrays Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 2 of 3. Q58 2210/23/O/N/24 original Q12 Structured · 15 marks Worked solution Whole program Plan: input and check each weight, sort both arrays together (largest first), output the top two, then count and store the names with weights over 3. // GroupSize is the number of members in the group // Input and validate each weight (more than 0 and under 15 kg) FOR Index ← 1 TO GroupSize REPEAT OUTPUT "Enter the weight in kg picked by ", PickerName[Index], ": " INPUT PickedWeight[Index] IF PickedWeight[Index] <= 0 OR PickedWeight[Index] >= 15 THEN OUTPUT "Invalid weight. Enter a weight above 0 and below 15." ENDIF UNTIL PickedWeight[Index] > 0 AND PickedWeight[Index] < 15 NEXT Index // Bubble sort, largest weight first, swapping names and weights together Last ← GroupSize REPEAT Swap ← FALSE FOR Index ← 1 TO Last - 1 IF PickedWeight[Index] < PickedWeight[Index + 1] THEN TempWeight ← PickedWeight[Index] PickedWeight[Index] ← PickedWeight[Index + 1] PickedWeight[Index + 1] ← TempWeight TempName ← PickerName[Index] PickerName[Index] ← PickerName[Index + 1] PickerName[Index + 1] ← TempName Swap ← TRUE ENDIF NEXT Index Last ← Last - 1 UNTIL NOT Swap OR Last = 1 // After sorting, the first two are the heaviest OUTPUT "Best in Group: ", PickerName[1], " with ", PickedWeight[1], " kg" OUTPUT "Second best in Group: ", PickerName[2], " with ", PickedWeight[2], " kg" // Store names of members over 3 kg for certificates Count ← 0 FOR Index ← 1 TO GroupSize IF PickedWeight[Index] > 3 THEN Count ← Count + 1 PickerCertificate[Count] ← PickerName[Index] ENDIF NEXT Index OUTPUT "Number of certificates to be printed: ", Count Each weight input with a prompt in a loop [1] Weight validated (a sensible range) with the user asked again if invalid [2] Nested loops or a correct sorting method used to sort into descending order [1] Weights compared and swapped using a temporary variable [1] PickerName[] swapped at the same time so names stay with their weights [1] Best in Group and Second best in Group output with name and weight [2] Names of members with weight over 3 stored in PickerCertificate[] [2] Certificates counted and a message with the number output [1] Comments explain how the code works (several useful comments) [2] Meaningful identifier names used, matching the ones in the question [1] Solution is in a logical order and meets all the requirements [1] Any correct pseudocode or program code that does the same job scores. Minor syntax errors are ignored. KEY IDEA Sort the two arrays together so names stay with weights, then the top two are at the start. TOTAL 15 marks Official mark scheme VIDEO ▶ Topic lesson A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.