Skip to the worksheet
Revision Library
Choose a subject

1D and 2D Array Processing: Writing and Tracing Programs - Worksheets, Questions and Revision

16 original exam-style questions - 7 pages of questions with a full mark scheme - free printable PDF.

Grades 1-9

This topic is chapter 9 of GCSE Computer Science Topic Workbook 2 and chapter 18 of GCSE Computer Science 10-Minute Tests.

Revision Library
revisionlibrary.co.uk
GCSE · Computer Science

2.11 1D and 2D Array Processing: Writing and Tracing Programs

OCR J277 · Calculators not allowed · about 70 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
Explain the difference between accessing a single element of a 1D array and a single element of a 2D array.
(Total for Question 1 is 2 marks)
2
State one advantage of using a single 2D array, rather than several separate 1D arrays, to store a grid of related data such as a seating plan.
(Total for Question 2 is 2 marks)
3
Explain why it is more efficient for a program to store the press-up totals for a whole class in a single array, rather than in 30 separate variables.
(Total for Question 3 is 3 marks)
4
Using the bookings array from question 11, write pseudocode to calculate the total number of seats booked for screen 0 only (i.e. the total of just one row of the 2D array), storing the result in a variable called screenTotal.
(Total for Question 4 is 3 marks)
5
A weather station records rainfall, in mm, for 3 towns over 5 days in a 2D array called rainfall, where the first index is the town number (0 to 2) and the second index is the day number (0 to 4):
rainfall = [[2, 0, 5, 8, 1], [0, 0, 3, 4, 2], [6, 4, 7, 5, 3]]
Describe, using structured English or pseudocode, an algorithm to find which town (index 0, 1 or 2) had the highest total rainfall across the 5 days, and state the total for that town.
(Total for Question 5 is 6 marks)
6
The array visits stores the number of visitors to a museum over 3 sensors across 4 days: visits = [[120, 95, 0, 140], [80, 0, 60, 110], [0, 75, 90, 0]]
Write pseudocode using nested FOR loops to count how many of the 12 values in the array are equal to 0 (representing a day a sensor recorded no visitors), storing the result in a variable called zeroCount.
(Total for Question 6 is 5 marks)
7
The array prices holds the price, in pounds, of 4 items in a shop: prices = [20, 35, 12, 48]
Write pseudocode to apply a 10% discount to every price in the array (i.e. reduce each value by 10%), updating the array in place.
(Total for Question 7 is 3 marks)
8
The array temperatures stores 7 daily temperature readings (in degrees Celsius) for one week: temperatures = [14, 17, 12, 19, 21, 16, 13]
The pseudocode below is intended to calculate the total of all 7 temperatures.
total <- 0
FOR day <- 0 TO 6
    total <- total + temperatures[day]
NEXT day
OUTPUT total
(a)State the value that will be output by this algorithm.(2)
(b)Extend the algorithm to also calculate and output the mean (average) temperature for the week, to 1 decimal place.(2)
(Total for Question 8 is 4 marks)
9
The array attendance stores the number of pupils absent from school each day over 6 days: attendance = [3, 0, 5, 2, 0, 1]
(a)Write pseudocode to count how many of the 6 days had zero pupils absent (attendance[index] = 0), storing the result in a variable called perfectDays.(3)
(b)State the value that perfectDays will hold after your algorithm in part (a) has run.(1)
(Total for Question 9 is 4 marks)
10
The array windSpeeds holds 6 wind speed readings, in mph: windSpeeds = [18, 25, 9, 31, 14, 22]
Write pseudocode to find the highest wind speed in the array, storing it in a variable called strongest.
(Total for Question 10 is 4 marks)
11
A cinema stores the number of seats booked for 3 screens over 4 showings each in a 2D array called bookings, where the first index is the screen number (0 to 2) and the second index is the showing number (0 to 3):
bookings = [[45, 60, 38, 50], [22, 40, 55, 30], [70, 65, 80, 60]]
(a)State the value of bookings[1][2], and explain what it represents.(2)
(b)State the total number of individual booking values stored in the whole 2D array, and explain how you calculated it.(2)
(Total for Question 11 is 4 marks)
12
Using the bookings array from question 11, write pseudocode using nested FOR loops to calculate the total number of seats booked across all 3 screens and all 4 showings, storing the result in a variable called grandTotal.
(Total for Question 12 is 5 marks)
13
A register2D array stores whether each of 4 students attended each of 5 lessons (1 = present, 0 = absent), where the first index is the student and the second index is the lesson:
register2D = [[1, 1, 0, 1, 1], [1, 0, 0, 1, 0], [0, 0, 0, 0, 1], [1, 1, 1, 1, 1]]
A variable studentIndex already holds the row index of one particular student. Write pseudocode to count how many of the 5 lessons that one student attended, storing the result in a variable called lessonsAttended.
(Total for Question 13 is 4 marks)
14
The pseudocode below contains one error, which stops it correctly finding the lowest value in the array readings, where readings = [40, 12, 27, 8, 19]:
lowest <- readings[0]
FOR index <- 0 TO 4
    IF readings[index] < lowest THEN
        lowest <- index
    ENDIF
NEXT index
OUTPUT lowest
(a)Identify the line that contains the error, and explain what is wrong with it.(2)
(b)Write the corrected line of pseudocode.(1)
(Total for Question 14 is 3 marks)
15
The array percentages stores 6 exam percentages: percentages = [64, 88, 51, 73, 95, 68]
Write pseudocode to find both the highest and the lowest values in the array in a single pass through it, storing them in variables called highest and lowest.
(Total for Question 15 is 5 marks)
16
The pseudocode below reverses the order of the values in a 5 element array called letters, in place (without using a second array).
letters <- ["P", "L", "A", "N", "T"]
FOR left <- 0 TO 1
    right <- 4 - left
    temp <- letters[left]
    letters[left] <- letters[right]
    letters[right] <- temp
NEXT left
OUTPUT letters
(a)Complete the trace table below to show the value of letters after each pass of the loop.
leftrightletters
(start)-[P,L,A,N,T]
0
1
(3)
(b)Explain why the FOR loop only needs to run from 0 TO 1, rather than looping through every index in the array.(2)
(Total for Question 16 is 5 marks)
Mark scheme · 2.11 1D and 2D Array Processing: Writing and Tracing Programs

Question 1

  • a 1D array element is accessed using a single index, e.g. scores[3], oeB1
  • a 2D array element is accessed using two indices, one for the row and one for the column, e.g. grades[1][2], oeB1
  • Answer: A 1D array uses one index, e.g. scores[3]; a 2D array uses two indices, one for the row and one for the column, e.g. grades[1][2].

Question 2

  • a single 2D array keeps all the related data grouped together under one name, oeB1
  • this makes it possible to process the whole grid using nested loops, rather than writing separate code to handle several differently-named 1D arrays, oeB1
  • Answer: A 2D array keeps all the related data under one name, so nested loops can process the whole grid, rather than needing separate code for several differently-named 1D arrays.

Question 3

  • an array groups many related values together under one name, rather than needing a separate, uniquely-named variable for each one, oeB1
  • this makes it possible to process every value using a loop (e.g. a FOR loop over the array's indices), instead of writing separate code for every variable, oeB1
  • it also makes the program easier to extend, e.g. to a different class size, without having to add or rename lots of individual variables, oeB1
  • Answer: An array groups all the related values under one name, so a loop can process every value, and the program can easily scale to a different number of students without rewriting variable names.

Question 4

  • screenTotal <- 0 initialised before the loop, oeM1
  • a single FOR loop over the showing index, 0 TO 3, correctly using bookings[0][showing], oeM1
  • screenTotal <- screenTotal + bookings[0][showing] correctly placed inside the loop, loop correctly closedA1
  • Answer:
    screenTotal <- 0
    FOR showing <- 0 TO 3
        screenTotal <- screenTotal + bookings[0][showing]
    NEXT showing
    OUTPUT screenTotal

Question 5

  • Level 1 A basic or partial idea is given (e.g. only calculating one town's total, or only stating an answer with no method), with little use of correct pseudocode/structured English.1-2
  • Level 2 An algorithm is described that calculates the total rainfall for each town, using loops correctly for at least the row-total part, but the method for finding which town has the highest total is incomplete, unclear, or missing.3-4
  • Level 3 A complete, correctly ordered algorithm is described (in pseudocode or clear structured English) that calculates each town's total rainfall using a loop, then correctly identifies the town with the highest total (e.g. by comparing totals as they are calculated, similar to a find-the-maximum pattern), and the correct town and total (town 2, 25mm) are stated.5-6
  • Indicative content:
    • For each town (row), loop through its 5 days and add up the rainfall values to get that town's total
    • Keep track of the highest total seen so far, and which town it belongs to, updating them whenever a new town's total is higher
    • This is similar to the standard find-the-maximum pattern, but applied to a total calculated for each row rather than to single stored values
    • After checking all 3 towns, output the town with the highest total and its rainfall total
    • Working: town 0 total = 2+0+5+8+1 = 16; town 1 total = 0+0+3+4+2 = 9; town 2 total = 6+4+7+5+3 = 25
    • Town 2 has the highest total rainfall, with 25mm

Question 6

  • zeroCount <- 0 initialised before the loops, oeM1
  • outer FOR loop over the sensor index, 0 TO 2, oeM1
  • inner FOR loop over the day index, 0 TO 3, correctly nested, oeM1
  • correct IF condition, visits[sensor][day] = 0, oeM1
  • zeroCount <- zeroCount + 1 correctly placed inside the IF, and all loops/IF correctly closedA1
  • Answer:
    zeroCount <- 0
    FOR sensor <- 0 TO 2
        FOR day <- 0 TO 3
            IF visits[sensor][day] = 0 THEN
                zeroCount <- zeroCount + 1
            ENDIF
        NEXT day
    NEXT sensor
    OUTPUT zeroCount

Question 7

  • FOR loop over every index of the array, 0 TO 3, oeM1
  • correct discount calculation, e.g. prices[index] * 0.9, oeM1
  • prices[index] <- prices[index] * 0.9 correctly assigned back into the same element, loop correctly closedA1
  • Answer:
    FOR index <- 0 TO 3
        prices[index] <- prices[index] * 0.9
    NEXT index

Question 8

  • (a) correct method shown, e.g. 14+17+12+19+21+16+13, oeM1
  • (a) 112, caoA1
  • (a) Answer: 112
  • (b) mean <- total / 7 (or total / LEN(temperatures)) added after the loop, oeM1
  • (b) OUTPUT mean added, giving 16.0, awrtA1
  • (b) Answer:
    mean <- total / 7
    OUTPUT mean, giving 16.0

Question 9

  • (a) perfectDays <- 0 initialised before the loop, oeM1
  • (a) FOR loop over all 6 indices (0 TO 5) with a correct IF condition, attendance[index] = 0, oeM1
  • (a) perfectDays <- perfectDays + 1 correctly placed inside the IF, and NEXT/ENDIF correctly closedA1
  • (a) Answer:
    perfectDays <- 0
    FOR index <- 0 TO 5
        IF attendance[index] = 0 THEN
            perfectDays <- perfectDays + 1
        ENDIF
    NEXT index
  • (b) 2, cao, ft from part (a)B1
  • (b) Answer: 2

Question 10

  • strongest <- windSpeeds[0] correctly initialised to the first element, oeM1
  • FOR loop over the remaining indices (1 TO 5), oeM1
  • correct IF comparison, windSpeeds[index] > strongest, oeM1
  • strongest <- windSpeeds[index] correctly placed inside the IF, and loop correctly closedA1
  • Answer:
    strongest <- windSpeeds[0]
    FOR index <- 1 TO 5
        IF windSpeeds[index] > strongest THEN
            strongest <- windSpeeds[index]
        ENDIF
    NEXT index

Question 11

  • (a) 55, caoB1
  • (a) the number of seats booked for screen 1's (the second screen's) 3rd showing (showing index 2), oeB1
  • (a) Answer: 55, the number of seats booked for screen 1's showing at index 2.
  • (b) 3 x 4 shown as the method, oeM1
  • (b) 12, caoA1
  • (b) Answer: 12 (3 screens x 4 showings each)

Question 12

  • grandTotal <- 0 initialised before the loops, oeM1
  • outer FOR loop over the screen index, 0 TO 2, oeM1
  • inner FOR loop over the showing index, 0 TO 3, correctly nested inside the outer loop, oeM1
  • grandTotal <- grandTotal + bookings[screen][showing] correctly placed inside the inner loopA1
  • both loops correctly closed (NEXT showing before NEXT screen) and grandTotal output/available after both loops finishB1
  • Answer:
    grandTotal <- 0
    FOR screen <- 0 TO 2
        FOR showing <- 0 TO 3
            grandTotal <- grandTotal + bookings[screen][showing]
        NEXT showing
    NEXT screen
    OUTPUT grandTotal

Question 13

  • lessonsAttended <- 0 initialised before the loop, oeM1
  • a single FOR loop over the lesson index, 0 TO 4, correctly indexing register2D[studentIndex][lesson], oeM1
  • correct IF condition, register2D[studentIndex][lesson] = 1, oeM1
  • lessonsAttended <- lessonsAttended + 1 correctly placed inside the IF, loop/IF correctly closedA1
  • Answer:
    lessonsAttended <- 0
    FOR lesson <- 0 TO 4
        IF register2D[studentIndex][lesson] = 1 THEN
            lessonsAttended <- lessonsAttended + 1
        ENDIF
    NEXT lesson
    OUTPUT lessonsAttended

Question 14

  • (a) the line lowest <- index is identified as the errorB1
  • (a) it assigns the index of the smaller value to lowest, instead of the value itself (readings[index]), so lowest ends up holding a position number, not a reading, oeB1
  • (a) Answer: The line 'lowest <- index' is wrong; it should assign the value readings[index], not the index number itself.
  • (b) lowest <- readings[index]B1
  • (b) Answer: lowest <- readings[index]

Question 15

  • highest <- percentages[0] and lowest <- percentages[0] both correctly initialised to the first element, oeM1
  • a single FOR loop over the remaining indices, 1 TO 5, oeM1
  • correct IF condition to update highest, percentages[index] > highest, with highest <- percentages[index] correctly placed, oeM1
  • correct IF (or ELSEIF) condition to update lowest, percentages[index] < lowest, with lowest <- percentages[index] correctly placed, oeA1
  • both comparisons made within the same single loop (not two separate loops), with the loop correctly closedB1
  • Answer:
    highest <- percentages[0]
    lowest <- percentages[0]
    FOR index <- 1 TO 5
        IF percentages[index] > highest THEN
            highest <- percentages[index]
        ENDIF
        IF percentages[index] < lowest THEN
            lowest <- percentages[index]
        ENDIF
    NEXT index

Question 16

  • (a) left=0: right=4, letters becomes [T,L,A,N,P], caoB1
  • (a) left=1: right=3, letters becomes [T,N,A,L,P], caoB1
  • (a) OUTPUT = [T,N,A,L,P], caoB1
  • (a) Answer: After left=0: [T,L,A,N,P]; after left=1: [T,N,A,L,P]; OUTPUT = [T,N,A,L,P]
  • (b) each pass of the loop swaps a pair of elements (one from the left half, one from the right half), so only half of the array's indices need to be visited to swap every pair once, oeB1
  • (b) looping through every index would swap every pair twice, undoing the reversal and leaving the array back in its original order, oeB1
  • (b) Answer: Each pass swaps one pair of elements, so only half the indices need visiting; continuing further would swap each pair back, undoing the reversal.

Mark your answers

Check answers without an account. Some answers can be checked automatically; use the mark scheme to mark the rest.

Question 1

2 marks
Did your answer earn the marks?

Question 2

2 marks
Did your answer earn the marks?

Question 3

3 marks
Did your answer earn the marks?

Question 4

3 marks
Did your answer earn the marks?

Question 5

6 marks
Did your answer earn the marks?

Question 6

5 marks
Did your answer earn the marks?

Question 7

3 marks
Did your answer earn the marks?

Question 8

4 marks
Did your answer earn the marks?

Question 9

4 marks
Did your answer earn the marks?

Question 10

4 marks
Did your answer earn the marks?

Question 11

4 marks
Did your answer earn the marks?

Question 12

5 marks
Did your answer earn the marks?

Question 13

4 marks
Did your answer earn the marks?

Question 14

3 marks
Did your answer earn the marks?

Question 15

5 marks
Did your answer earn the marks?

Question 16

5 marks
Did your answer earn the marks?
Mark my answers

Worksheets are written for Revision Library and come with a full mark scheme; a few English packs quote an out-of-copyright text, and name it on the page. Exam board names and specification codes say which qualification a pack suits: we are not affiliated with, endorsed by or approved by any exam board, and no past-paper question is reproduced here. Rights queries: hello@revisionlibrary.co.uk