Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  One-dimensional arrays
Lesson pack · IGCSE Computer Science 0478 · 8.2 · IGCSE

One-dimensional arrays

One name, many values. Storing 30 test marks in 30 separate variables would be painful.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.2
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 9 of 11 in Programming; follows Writing maintainable programs; leads into Two-dimensional arrays and nested loops
Central model
Fig. 1: a one-dimensional array of five integers
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Declaring an array: The declaration gives the identifier, the range of indexes and the data type of every element.
  • Arrays and loops: In the lab, choose Maximum and minimum.
  • Using the index: The index can be any expression that gives an integer, so a program can search an array, or move values between neighbouring elements, such as Names[Count + 1].
Key points to leave on the board
  • Declare: DECLARE Name : ARRAY[1:n] OF type.
  • Access: Name[Index], where Index can be a variable or expression.
  • Loop: FOR Index ← 1 TO n to input, total or search every element.
  • Max and min: start with the first element, then compare the rest.
The core visual model

One picture every idea returns to

An array is a data structure that holds a fixed number of elements of the same data type under one identifier. Each element is reached by its index, written in square brackets: Scores[3]. Declare it with its lower and upper bounds: DECLARE Scores : ARRAY[1:5] OF INTEGER. The first index can be 0 or 1 (exam pseudocode generally uses 1). Because the index can be a variable, a FOR loop can visit every element in turn.

DECLARE Scores : ARRAY[1:5] OF INTEGER Scores 12345 12715911 index Scores[3] = 15 one identifier, five elements of the same data type, each reached by its index
Fig. 1: a one-dimensional array of five integers
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, One name, many values.. Storing 30 test marks in 30 separate variables would be painful. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Declaring an arrayThe declaration gives the identifier, the range of indexes and the data type of every element.Whole class, then pairs
13 to 21 minTeach and check: Arrays and loopsIn the lab, choose Maximum and minimum. Project tab 2 of the lab (Arrays with standard methods) and let a student drive it while the class predicts. Model the worked example Finding the largest on the board, one line at a time.Pairs
21 to 30 minTeach and check: Using the indexThe index can be any expression that gives an integer, so a program can search an array, or move values between neighbouring elements, such as Names[Count + 1]. Model the worked example Linear search on the board, one line at a time. Your turn: Count is 2. Which element does Names[Count + 1] refer to?Think-Pair-Share
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 19 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Using the index, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Think-Pair-Share

Pose the question, give thirty seconds of silent thinking, then a minute to compare with a partner before a few pairs share with the class. The silent minute is the part that cannot be cut.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Using an index outside the bounds, such as Scores[6] or Scores[0] for ARRAY[1:5], is an error: that element does not exist.Return to Declaring an array: ask a pair to state the correct version and show it on the lab before moving on.
Start Highest at the first element, not at 0: if every value were negative, a starting value of 0 would be wrongly output as the largest.Return to Arrays and loops: ask a pair to state the correct version and show it on the lab before moving on.
Worksheet

One-dimensional arrays: worksheet

6 questions, 19 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[3 marks]

Describe what is meant by a one-dimensional array.

Q2 · write[3 marks]

Write pseudocode to declare an array Heights to store 20 real values and use a loop to input all 20 values.

Q3 · write[4 marks]

The array Prices[1:50] holds 50 prices. Write pseudocode to find and output the lowest price.

Q4 · trace[3 marks]

State the values stored in Data and the output.

FOR Index ← 1 TO 4
    Data[Index] ← Index * Index - 1
NEXT Index
OUTPUT Data[2] + Data[4]
Q5 · write[4 marks]

The array Marks[1:30] holds 30 marks. Write pseudocode to count and output how many marks are 50 or more.

Q6 · explain[2 marks]

Explain why storing 30 names in an array is better than using 30 separate variables.

Answer key and marking notes
Q1 · 3 marks
  • A data structure with a fixed number of elements under a single identifier.
  • All the elements are of the same data type.
  • Each element is accessed using an index (its position).
Q2 · 3 marks
  • DECLARE Heights : ARRAY[1:20] OF REAL
  • FOR Index ← 1 TO 20 ... NEXT Index
  • INPUT Heights[Index] inside the loop
Q3 · 4 marks
  • Lowest ← Prices[1]
  • FOR Index ← 2 TO 50
  • IF Prices[Index] < Lowest THEN Lowest ← Prices[Index]
  • After the loop: OUTPUT Lowest
Q4 · 3 marks
  • Data[1] = 0, Data[2] = 3.
  • Data[3] = 8, Data[4] = 15.
  • Output: 3 + 15 = 18
Q5 · 4 marks
  • Count ← 0 before the loop
  • FOR Index ← 1 TO 30
  • IF Marks[Index] >= 50 THEN Count ← Count + 1
  • After the loop: OUTPUT Count
Q6 · 2 marks
  • Only one identifier is needed, so the code is shorter and easier to read.
  • A loop with the index as the loop variable can process every name with the same statements.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

An array is declared with DECLARE Temps : ARRAY[1:7] OF REAL. Which statement is correct?

  • AIt holds 7 elements, and every element is REAL
  • BIt holds 8 elements, and every element is REAL
  • CIt holds 7 elements, and each element can be any data type
  • DIt holds a single REAL value that is 7 digits long
Q21 mark

Scores is ARRAY[1:5] OF INTEGER and holds 12, 7, 15, 9, 11 in that order. What is output by OUTPUT Scores[2] + Scores[4]?

  • A16
  • B26
  • C6
  • D22
Q31 mark

Scores is declared as ARRAY[1:5] OF INTEGER. Which statement causes an error when it runs?

  • AScores[0] ← 4
  • BScores[5] ← 0
  • CScores[1] ← Scores[5]
  • DScores[3] ← Scores[3] + 1
Q41 mark

Scores holds 4, 9, 2, 9, 6 and the input is 9. What is output?

INPUT Target
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 5 DO
    IF Scores[Index] = Target
      THEN
        Found ← TRUE
      ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE
IF Found = TRUE
  THEN
    OUTPUT "Found at position ", Index
  ELSE
    OUTPUT "Not found"
ENDIF

  • AFound at position 2
  • BFound at position 4
  • CFound at position 9
  • DNot found
Q51 mark

Temps is ARRAY[1:4] OF INTEGER and holds -3, -8, -1, -5. What is output?

Highest ← 0
FOR Index ← 1 TO 4
    IF Temps[Index] > Highest
      THEN
        Highest ← Temps[Index]
    ENDIF
NEXT Index
OUTPUT Highest

  • A0
  • B-1
  • C-8
  • D-3
Q61 mark

Data is ARRAY[1:5] OF INTEGER and holds 3, 8, 1, 6, 4. The code is meant to move every value one place to the right. What does Data hold afterwards?

FOR Index ← 2 TO 5
    Data[Index] ← Data[Index - 1]
NEXT Index

  • A3, 3, 3, 3, 3
  • B3, 3, 8, 1, 6
  • C8, 1, 6, 4, 4
  • D3, 8, 1, 6, 4
Quiz key
Q1: A

The bounds 1 to 7 give 7 elements, Temps[1] to Temps[7], and every element of an array has the same data type, here REAL. 8 would be the count for bounds 0 to 7. An array cannot mix data types. The bounds set the number of elements, not the size of one value.

Q2: A

Scores[2] is 7 and Scores[4] is 9, so 7 + 9 = 16. 26 counts from 0 (15 + 11). 6 adds the indexes instead of the values. 22 uses the elements at positions 2 and 3 (7 + 15).

Q3: A

Index 0 is below the lower bound of 1, so Scores[0] does not exist. Scores[5] is the last element and is valid. Copying Scores[5] into Scores[1] uses two valid indexes. Adding 1 to Scores[3] reads and writes a valid element.

Q4: A

Scores[1] is 4, so Index becomes 2; Scores[2] is 9, so Found becomes TRUE and the loop stops with Index = 2. Position 4 holds the second 9, which is never reached. 9 is the value, not its position. Not found would need no element to equal 9.

Q5: A

Every element is negative, so no Temps[Index] > 0 is ever TRUE and Highest keeps its starting value: 0 is output, although 0 is not in the array. Starting with Highest ← Temps[1] would give the correct largest, -1. -8 is the smallest value. -3 is the first element.

Q6: A

Pass 1 copies Data[1] (3) into Data[2]; pass 2 copies the new Data[2] (3) into Data[3], and so on, so every element ends up 3. 3, 3, 8, 1, 6 is the intended result, which needs the loop to run backwards from 5 down to 2. 8, 1, 6, 4, 4 is a shift to the left. 3, 8, 1, 6, 4 assumes nothing changes.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonWriting maintainable programs