Computer ScienceExam code: 0984

Arrays

Programming

A parent records the minutes of TV watched by 4 children (Quinn, Lyla, Harry, Elias) over 5 weekdays (Monday to Friday). The data is held in a 2D array called MinsWatched indexed as [Day, Child], where days 1 to 5 are Monday to Friday and children 1 to 4 are Quinn, Lyla, Harry and Elias.

1 (Quinn)2 (Lyla)3 (Harry)4 (Elias)
1 Monday34678978
2 Tuesday56434556
3 Wednesday122233445
4 Thursday131092390
5 Friday4710016723

Reading a single element uses the row-then-column order:

  • Lyla on Tuesday: OUTPUT MinsWatched[2, 2] outputs 43.
  • Harry on Friday: OUTPUT MinsWatched[5, 3] outputs 167.
  • Quinn on Wednesday: OUTPUT MinsWatched[3, 1] outputs 122.

Calculating each child's weekly total

FOR Child ← 1 TO 4
   ChildTotal ← 0
   FOR Day ← 1 TO 5
      ChildTotal ← ChildTotal + MinsWatched[Day, Child]
   NEXT Day
   OUTPUT "Child ", Child, ": ", ChildTotal, " minutes"
NEXT Child

For Quinn (Child 1): 34 + 56 + 122 + 13 + 47 = 272 minutes.

Finding the day with the most total viewing

DECLARE MaxTotal : INTEGER
DECLARE MaxDay : INTEGER
MaxTotal ← 0
MaxDay ← 0

FOR Day ← 1 TO 5
   DayTotal ← 0
   FOR Child ← 1 TO 4
      DayTotal ← DayTotal + MinsWatched[Day, Child]
   NEXT Child
   IF DayTotal > MaxTotal
     THEN
        MaxTotal ← DayTotal
        MaxDay ← Day
   ENDIF
NEXT Day

OUTPUT "Highest day was ", MaxDay, " with ", MaxTotal, " minutes total"

Common exam question

Using arrays in the 15-mark program

Question: Write a commented program for a scenario whose data sits in named 1D or 2D arrays, meeting a list of requirements, with messages on every input and output (15 marks).

Set in 8 of the 17 papers: every Paper 2 ends with it, always with the data in arrays. It is level-marked (up to 9 marks for techniques and data structures, up to 6 for comments, meaningful names and logic), and the top level needs data structures that store all the data required. Use each array name exactly as given, as every scheme requires.

When an item's data shares an index across the arrays, one loop variable drives them all: Pupils[Index], Marks[Index, 2] and Grades[Index] belong to one pupil. Store calculated values (an area, a points total) in their array too. When a variable says how many items are stored, loop to it, not to the array's full size, and put each new entry at the next free index.

Worked example

Storing the items that pass a test in a second array

The parallel arrays Players[1:6] and Goals[1:6] hold six players' names and their goals this season, so the goals at index 3 of Goals belong to the player at index 3 of Players. Players holds Ava, Ben, Cara, Dev, Ella and Finn, and Goals holds 12, 7, 10, 3, 15, 9. Write pseudocode that copies the name of every player with at least 10 goals into the array Award[1:6] and outputs how many awards there are. What does Award hold afterwards?

Solution:

  • Set a counter to 0 before the loop. It counts the awards and also gives the next free position in Award.
  • Visit every index from 1 to 6. When Goals[Index] is at least 10, add 1 to the counter first, then copy Players[Index] into Award[Count].
  • Output the counter once, after the loop.
Count ← 0
FOR Index ← 1 TO 6
   IF Goals[Index] >= 10
     THEN
       Count ← Count + 1
       Award[Count] ← Players[Index]
   ENDIF
NEXT Index
OUTPUT "Number of awards: ", Count
  • Index 1: 12 goals, so Count becomes 1 and Ava goes into Award[1]
  • Index 3: exactly 10 goals, which >= accepts, so Count becomes 2 and Cara goes into Award[2]
  • Index 5: 15 goals, so Count becomes 3 and Ella goes into Award[3]; indexes 2, 4 and 6 fail the test
  • Output: Number of awards: 3, with Award holding Ava, Cara, Ella in elements 1 to 3

Placing each name at Award[Index] instead would leave gaps (Ava at 1, Cara at 3, Ella at 5), and > in place of >= would lose Cara.

Build on this topic