Skip to content
SPM Tuition
Computer Science · Advanced programming practice

Working with arrays and structured data

You know what an array is but your loop skips the first value or runs off the end.

An array stores several values under one name, and each value is reached by its index. Loops and arrays go together: one loop visits every index, so the loop bounds decide whether you visit every value exactly once.

This lesson starts advanced programming practice. If variables and data types still feel shaky, revisit variables, constants and data types first.

How do I read an array?

Draw it as a row of boxes with the index above and the value inside. Do this before you read any code, because most array mistakes are index mistakes.

Take an array called marks with five values, indexed from 0.

Index 0 1 2 3 4
marks 72 55 90 64 81

marks[2] is 90, because the index counts from 0. The array has 5 elements, so the last index is 4, not 5.

Worked example 1: total and average

Write the steps to find the total and average of the marks.

total = 0
FOR i = 0 TO 4
    total = total + marks[i]
NEXT i
average = total / 5

Trace it: 72, then 127, then 217, then 281, then 362. The total is 362, so the average is 362 ÷ 5 = 72.4.

The loop runs from 0 to 4, which is five steps, and the division also uses 5. Both come from the same fact: the array has five elements.

The mistake that misses or overshoots

A common slip is to loop up to the number of elements instead of the last index. It feels natural because the count is the number you know.

Loop Indexes visited Result
FOR i = 0 TO 5 0, 1, 2, 3, 4, 5 marks[5] does not exist, so a runtime error or a garbage value
FOR i = 1 TO 4 1, 2, 3, 4 marks[0] = 72 is skipped, so the total is 290
FOR i = 0 TO 4 0, 1, 2, 3, 4 Correct: 362

Both wrong loops look fine on a quick read. A trace table with an index column shows the skipped or extra step at once.

Worked example 2: structured data

Suppose each student has a name and a mark. A record groups them.

Record name mark
student[0] Aina 72
student[1] Ben 55
student[2] Chong 90

To print the names of students with a mark of 60 or more:

FOR i = 0 TO 2
    IF student[i].mark >= 60 THEN
        PRINT student[i].name
    ENDIF
NEXT i

The output is Aina, then Chong. A record keeps student[i].name and student[i].mark together for the same person. Two separate arrays can fall out of step, for example if one is sorted and the other is not.

A routine for any array question

  1. Write the index row and the value row.
  2. Write the number of elements and the last valid index.
  3. Copy the loop bounds beside them and confirm they match.
  4. Trace the first step and the last step before you trace the middle.

Check yourself

An array temps holds 28, 31, 30, 27 at indexes 0 to 3. What does this code print, and what is wrong if the loop were FOR i = 0 TO 4?

hot = 0
FOR i = 0 TO 3
    IF temps[i] > 29 THEN
        hot = hot + 1
    ENDIF
NEXT i
PRINT hot
Answer

Trace: i = 0 gives 28, not above 29, so hot stays 0. i = 1 gives 31, so hot is 1.

Then i = 2 gives 30, so hot is 2. Finally i = 3 gives 27, so hot stays 2. The code prints 2.

With FOR i = 0 TO 4, the loop would read temps[4], which does not exist for an array with 4 elements. Depending on the language, that is a runtime error or an invalid value. The last index of a 4-element array is 3.

What to study next

Next, use arrays inside algorithms: tracing searching and sorting algorithms within scope. You can also practise following code line by line with the restricted pseudocode trace trainer.

If you want a teacher to trace your own array code with you, see online one-to-one Computer Science tuition.

Common questions

Does an array start at index 0 or index 1?

It depends on the language and on the notation your exam or teacher uses. Python, Java and C++ start at 0. Check the convention given in the question, and write the first index beside your array before you loop.

What is the difference between an array and a variable?

A variable holds one value. An array holds several values of the same kind under one name, and you pick one by its index. That lets a loop process every value with the same few lines.

What is structured data in this context?

Data grouped so that related values stay together, such as a record with a name and a mark. A list of such records lets you keep each student's values linked instead of in separate unrelated arrays.

How do I avoid running past the end of an array?

Write the number of elements, then write the last valid index next to it. For n elements starting at 0, the last index is n − 1. Use that as the loop limit and trace the final step.

If your loops are nearly right but miss one element, one-to-one Computer Science lessons let a teacher trace your own code and show exactly which index you skip.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.