Procedures, Functions, Arrays and File Handling: Question 4

Syllabus 8.2

Structured 7 marks

A school app stores the quiz marks for 3 students across 4 quizzes in a two-dimensional array, Marks, declared as ARRAY[1:3, 1:4], where the first index is the student number and the second index is the quiz number.

DECLARE Marks : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Student : INTEGER
DECLARE Quiz : INTEGER
DECLARE RowTotal : INTEGER
DECLARE ColumnTotal : INTEGER

Marks[1,1] ← 8
Marks[1,2] ← 6
Marks[1,3] ← 9
Marks[1,4] ← 7
Marks[2,1] ← 5
Marks[2,2] ← 7
Marks[2,3] ← 6
Marks[2,4] ← 8
Marks[3,1] ← 9
Marks[3,2] ← 9
Marks[3,3] ← 8
Marks[3,4] ← 10

(a) State the value of Marks[2,3]. [1]

(b) The nested loop below calculates and outputs the total mark for each student, by adding together all four of their quiz marks.

FOR Student ← 1 TO 3
    RowTotal ← 0
    FOR Quiz ← 1 TO 4
        RowTotal ← RowTotal + Marks[Student, Quiz]
    NEXT Quiz
    OUTPUT RowTotal
NEXT Student

Trace this nested loop and state the three values that are output, in order. [3]

(c) A second version of the program swaps the order of the two loops, as shown below, to calculate a total for each quiz instead.

FOR Quiz ← 1 TO 4
    ColumnTotal ← 0
    FOR Student ← 1 TO 3
        ColumnTotal ← ColumnTotal + Marks[Student, Quiz]
    NEXT Student
    OUTPUT ColumnTotal
NEXT Quiz

State what real-world quantity each value of ColumnTotal represents, and calculate the value of the first output (for Quiz 1). [2]

(d) In the array declaration ARRAY[1:3, 1:4], state which index, the first or the second, represents the student number, and which represents the quiz number. [1]

Show worked solution Hide worked solution

Worked solution

Part (a): Reading a single element of a 2D array

Marks[2,3] means row 2 (Student 2), column 3 (Quiz 3). Reading directly from the declarations, Marks[2,3] ← 6, so Marks[2,3] = 6.

Part (b): Tracing the nested loop that totals each student’s marks

For each Student, RowTotal is reset to 0, then the inner loop adds each of that student’s 4 quiz marks in turn:

  • Student 1: Marks[1,1..4] = 8, 6, 9, 7 → RowTotal = 8+6+9+7 = 30
  • Student 2: Marks[2,1..4] = 5, 7, 6, 8 → RowTotal = 5+7+6+8 = 26
  • Student 3: Marks[3,1..4] = 9, 9, 8, 10 → RowTotal = 9+9+8+10 = 36

The three values output, in order, are 30, 26, 36.

Part (c): Tracing the swapped loop that totals each quiz

With the loops swapped, Quiz is now the outer variable, and ColumnTotal is reset to 0 at the start of every pass through the outer loop, before the inner loop sums that one quiz’s mark from all 3 students. So each value of ColumnTotal represents the total mark achieved by all 3 students on one particular quiz (rather than one student’s total across all quizzes, as in part (b)).

For Quiz 1 specifically: ColumnTotal = Marks[1,1] + Marks[2,1] + Marks[3,1] = 8 + 5 + 9 = 22.

(For completeness, tracing all four quizzes gives 22, 22, 23 and 25, and these four values add to 92, exactly matching the sum of the three row totals from part (b), 30+26+36=92, since both methods add up the same 12 marks, just in a different order.)

Part (d): Identifying which index is which

Marks was declared as ARRAY[1:3, 1:4] and filled using Marks[Student, Quiz], with Student running from 1 to 3 and Quiz running from 1 to 4. So the first index inside the square brackets is the student number, and the second index is the quiz number.

Final answers

  • (a) Marks[2,3] = 6
  • (b) Outputs, in order: 30, 26, 36
  • (c) Each ColumnTotal = total mark for one quiz across all 3 students; first output = 22
  • (d) First index = student number; second index = quiz number