GCSE Computer Science · Topic guide

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

An array is a data structure that stores many items of the same data type under one identifier, with each item reached by its index. Indexing starts at 0 in the languages used at GCSE, so an array of 5 items has indexes 0 to 4 and the last index is always the length minus one. A one dimensional array is a single list; a two dimensional array is a table addressed by two indexes, conventionally array[row][column], which is how a seating plan, a grid of scores or a game board is stored. Almost every array question is solved with the same three patterns: a single for loop over a 1D array, a nested for loop over a 2D array where the outer loop moves through the rows and the inner loop through the columns, and a running variable set up before the loop that is updated inside it. Tracing an array algorithm means writing a trace table with a column for each variable and one row per pass of the loop.

GCSE Grades 1-9Paper 2: Computational Thinking, Algorithms and ProgrammingOCR J277AQAEdexcelWJECEduqas

Before you start

Make sure you're comfortable with these topics first:

Method

  1. Write down the structure before you write any code: how many dimensions, what each index means, and what the valid index range is. For a 1D array of n items the range is 0 to n - 1.
  2. Set up any running variables before the loop begins. A total starts at 0, a counter starts at 0, and a running maximum or minimum starts at the first element of the array rather than at 0, so that negative values still work.
  3. Choose the loop. Use a single for loop to visit every item of a 1D array, and a nested for loop for a 2D array, with the outer loop over rows and the inner loop over columns.
  4. Inside the loop, refer to the current element through the loop variable, such as scores[i] or grid[row][col], never by a fixed index.
  5. For a search, compare each element with the target and stop or record the index when it matches. For a total or a maximum, update the running variable each pass.
  6. To trace, draw a table with one column per variable plus one for the array element being read, and complete one row per pass. Work strictly line by line and never skip ahead to the answer you expect.

Worked example

A 2D array called marks holds the test scores of 3 pupils across 4 tests, so marks[pupil][test]. Write an algorithm that outputs each pupil's total, and then outputs the highest single score in the whole array along with the pupil and test it belongs to.

  1. Establish the index ranges. There are 3 pupils, so the row index runs 0 to 2; there are 4 tests, so the column index runs 0 to 3.
  2. Set up the running variables before any loop: highest = marks[0][0], bestPupil = 0, bestTest = 0. Starting from the first element rather than 0 means the algorithm still works if every score is 0.
  3. Write the outer loop over pupils: for pupil = 0 to 2. Inside it, and before the inner loop, set total = 0, because each pupil needs a fresh total.
  4. Write the inner loop over tests: for test = 0 to 3. Inside it, add the current element to the total with total = total + marks[pupil][test].
  5. Still inside the inner loop, check for a new maximum: if marks[pupil][test] > highest then set highest = marks[pupil][test], bestPupil = pupil and bestTest = test.
  6. After the inner loop closes but still inside the outer loop, output the pupil's total. After both loops close, output highest, bestPupil and bestTest. Placing the total output inside the outer loop and the maximum output after both loops is what makes the two results come out at the right time.

Practice questions

Try each question, then tap to reveal the answer.

Q1An array named temps holds 12 values. State the index of the first item and the index of the last item.Show answer

Answer: First index 0, last index 11.

Got it right?
Q2Write a loop in pseudocode that outputs every element of a 1D array named names of length 6.Show answer

Answer: for i = 0 to 5\n print(names[i])\nnext i

Got it right?
Q3Explain why a running maximum should be initialised to the first element of the array rather than to 0.Show answer

Answer: If every value in the array is negative, a maximum initialised to 0 would never be replaced and the algorithm would wrongly report 0. Starting from the first element guarantees the answer is a value that is actually in the array.

Got it right?
Q4A 2D array grid is declared as grid[4][6]. State how many rows and columns it has and how many elements it holds in total.Show answer

Answer: 4 rows and 6 columns, so 24 elements in total.

Got it right?
Q5Describe what a nested loop does when it processes a 2D array.Show answer

Answer: The outer loop moves through the rows one at a time. For each row, the inner loop runs completely, visiting every column of that row, before the outer loop advances to the next row.

Got it right?
Q6Trace this algorithm with data = [4, 9, 2, 9, 7] and state the final output. total = 0; count = 0; for i = 0 to 4: if data[i] > 5 then total = total + data[i] and count = count + 1; next i; print(total, count).Show answer

Answer: Pass 0: 4 is not greater than 5, no change. Pass 1: 9 is added, total 9, count 1. Pass 2: 2 is not added. Pass 3: 9 is added, total 18, count 2. Pass 4: 7 is added, total 25, count 3. Output: 25 and 3.

Got it right?
Q7Explain what happens if a program tries to read index 5 of an array declared with 5 elements.Show answer

Answer: The valid indexes are 0 to 4, so index 5 is outside the array. The program will produce an index out of range error and stop, because it is trying to access memory that does not belong to the array.

Got it right?

Exam-style questions

Written in the style of a GCSE Computer Science exam paper, with a full mark scheme.

Q1[8 marks]

A 1D array called scores holds 10 integers. (a) Write an algorithm in pseudocode or a high level language that calculates and outputs the mean of the values. (b) Extend it so that it also outputs how many scores are above the mean. (c) State one test you would run to check the algorithm works, and give the expected result.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 8 available

Got it right?
Q2[8 marks]

A cinema stores its seat bookings in a 2D array called seats with 8 rows and 12 seats per row. An empty seat holds 0 and a booked seat holds 1. Write an algorithm that outputs the number of empty seats in each row, and then outputs the row number of the row with the most empty seats.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 8 available

Got it right?

See real GCSE Computer Science past-paper questions, with official mark schemes

Free printable worksheet

Want more practice on paper? Download the 1d and 2d array processing: writing and tracing programs worksheet pack - 14 pages of exam-style questions with a full mark scheme. One email opens every download in this browser for 14 days - no account, no card. Print it for personal and classroom use.

Next topics

Ready to practise 1d and 2d array processing: writing and tracing programs? Add it to a printable topic pack for this student in the Pack Builder.

Add to my pack

Not quite what you needed?

Tell us what is missing on 1d and 2d array processing: writing and tracing programs, or which topic to write up next. Every request is read, and we reply to every one.

Build a full practice pack.

This topic is one of hundreds in the library - pick the ones a student needs and generate a printable PDF in minutes.