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.
1D and 2D Array Processing: Writing and Tracing Programs revision guideMore GCSE Computer Science practiceMock exams
This topic is chapter 9 of GCSE Computer Science Topic Workbook 2 and chapter 18 of GCSE Computer Science 10-Minute Tests.
2.11 1D and 2D Array Processing: Writing and Tracing Programs
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.
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.
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.
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 totalWrite pseudocode to find the highest wind speed in the array, storing it in a variable called strongest.
bookings = [[45, 60, 38, 50], [22, 40, 55, 30], [70, 65, 80, 60]]
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.
lowest <- readings[0]
FOR index <- 0 TO 4
IF readings[index] < lowest THEN
lowest <- index
ENDIF
NEXT index
OUTPUT lowestWrite 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.
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| left | right | letters |
|---|---|---|
| (start) | - | [P,L,A,N,T] |
| 0 | ||
| 1 |
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.