Study Guide Instructor view

Arrays

Teaching notes for Lesson 5.10, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Intermediate
Time
About 90 minutes; the steps below add up to 90

Pupils start from a corridor of numbered lockers and discover that one shared name plus a number is better than unrelated names, before arrays, elements and indexes are met. They then fill and total an array with loops, find the highest score and its position, move to a two-dimensional seating plan, and visit every seat with nested loops. Almost every question can be settled by drawing the array as a row or grid of boxes with index numbers and writing values into it as the program runs.

Objectives

  • Explain what an array is, and why a program uses one instead of many separate variables
  • Declare a one-dimensional and a two-dimensional array in Cambridge pseudocode, stating the bounds
  • Read and write an element using an index, including a variable as the index
  • Use iteration to fill an array, to total its elements and to find the highest and its position
  • Use nested iteration to work through a two-dimensional array
  • Spot and correct index mistakes, including an index outside the bounds

Before the lesson

  • Mini whiteboards, one per pupil, for predictions.
  • Draw an empty row of five boxes numbered 1 to 5 and an empty 3 by 4 grid (rows 1 to 3, seats 1 to 4) on the board before the lesson.
  • Optional: twelve counters or sticky notes for seats, so that pupils can mark the X positions physically.
  • Copy the Scores, highest-score and seat-counting programs onto the board so that you can trace them with the pen.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Explain what an array is, and why a program uses one instead of many separate variables
    • Declare a one-dimensional and a two-dimensional array in Cambridge pseudocode, stating the bounds
    • Read and write an element using an index, including a variable as the index
    • Use iteration to fill an array, to total its elements and to find the highest and its position
    • Use nested iteration to work through a two-dimensional array
    • Spot and correct index mistakes, including an index outside the bounds

    Purpose: Pupils start from a corridor of numbered lockers and discover that one shared name plus a number is better than unrelated names, before arrays, elements and indexes are met. They then fill and total an array with loops, find the highest score and its position, move to a two-dimensional seating plan, and visit every seat with nested loops. Almost every question can be settled by drawing the array as a row or grid of boxes with index numbers and writing values into it as the program runs.

  2. 1. A row of lockers Think 5 min

    Purpose: Pupils see for themselves why one shared name with a number beats unrelated names, before the word array is used.

    Ask

    • Why is a shared description with a number better than names such as Maple, Otter and Jupiter?
    • How would you check every locker at the end of term?

    Responses to expect

    • The number says which one and the numbers run in order
    • Go through them one, two, three

    Misconceptions to challenge

    • A learner may believe: “Unrelated names are fine because each is unique.” Respond: Ask how you would say the next locker, or check all thirty, with names that have no order.

    Checking understanding: Pupils write one advantage on a mini whiteboard; look for order or counting.

    Depending on the answers

    • If pupils talk only about easier to remember: ask what a loop would need in order to visit all thirty.

    English as an additional language

    • Check locker and corridor; show a picture of a row of lockers.

    Discussion pause: Thirty seconds of silent thinking, then pairs give one advantage of numbering.

    In the lesson window

    • Think it through: Why is it better for the lockers to share one description (“locker”) with a number on each, than for every locker to have an unrelated name such as Maple, Otter or Jupiter? Think about telling a new pupil, and about checking every locker at the end of term.
  3. 2. Storing a list under one name Discover 14 min

    Purpose: Pupils meet the problem of 500 separate variables, then arrays, elements, indexes and bounds, practise reading elements and work out which statement is out of bounds.

    Ask

    • What goes wrong with Score1 to Score500, and what would you like to write instead?
    • What do Scores[1] + Scores[2] and Scores[Position + 1] (Position is 2) give?
    • Which statement refers to an element that does not exist?

    Responses to expect

    • 500 names and no loop through them; one name for the list
    • 19 and 15
    • Scores[6] ← 4

    Misconceptions to challenge

    • A learner may believe: “The index and the value are the same thing.” Respond: In Scores[3] the 3 is the place and 15 is the value; players' scores change while the places stay the same.
    • A learner may believe: “Scores[Position] when Position is 5 is out of bounds.” Respond: 5 is the upper bound, so Scores[5] exists.
    • A learner may believe: “Arrays can grow as values are added.” Respond: The size is fixed when it is declared.
    • A learner may believe: “The first index must be 1.” Respond: It can be 0 or 1; read the declaration.

    On the board or with the pen

    • Draw five boxes numbered 1 to 5 and write 12, 7, 15, 9, 13 in them; write the index above each box and the value inside it.

    What learners should notice

    • Both bounds are included: ARRAY[1:5] has five elements.

    Checking understanding: The out-of-bounds check: Scores[1] tests the lower bound; the variable-index option tests whether a variable can be an index at the upper bound.

    Depending on the answers

    • If pupils choose Scores[Position] with Position 5: ask them to count the boxes and say which index is the last.
    • If pupils are secure: ask how many elements ARRAY[0:9] has and which indexes are valid.

    Support

    • Provide the array drawn as boxes with the indexes written above, so that pupils read values off it.

    Stretch

    • If Scores is ARRAY[1:5], what does Scores[Scores[2] - 6] refer to?

    English as an additional language

    • Check element, index and bounds; use house numbers on a street as an example of an index.

    In the lesson window

    • Think it through: A fictional quiz has five players, and the program must remember each player’s score until the end. With ordinary variables you would need Score1, Score2, Score3, Score4 and Score5. What goes wrong if there are 500 players, and what would you like to be able to write instead?
    • Check your understanding: Scores is declared ARRAY[1:5] OF INTEGER. Which statement refers to an element that does not exist?
  4. 3. Filling and adding up an array Predict 16 min

    Purpose: Pupils predict a total from two loops over an array, trace how the array fills up, follow a worked example that finds the highest score and its position, and say what BestPosition holds.

    Ask

    • For scores 12, 7, 15, 9, 13, what is the output, and why two loops?
    • How does the array look after each pass of the first loop?
    • At the end, what does BestPosition hold?

    Responses to expect

    • Total: 56
    • The array keeps the scores so they can be used again
    • 3

    Misconceptions to challenge

    • A learner may believe: “BestPosition holds the highest score.” Respond: That is Highest. BestPosition holds the index where the highest was found.
    • A learner may believe: “BestPosition is 5 because the loop finishes at 5.” Respond: It is updated only when a higher score is found; that last happened at position 3.
    • A learner may believe: “The loop for the highest should start at 1 as well.” Respond: Position 1 has already been dealt with by Highest ← Scores[1], so it starts at 2.
    • A learner may believe: “INPUT Scores[Position] puts every value in Scores[1].” Respond: Position changes each pass, so each value goes into a different element.

    On the board or with the pen

    • Fill the five-box array pass by pass in the fill trace. Then trace Highest and BestPosition with columns Position, Scores[Position], Highest, BestPosition, crossing out and rewriting when a new highest is found.

    Checking understanding: The best-position check separates value (15) from index (3) and from the last loop value (5); the highest-trace table checks whether pupils update both variables together.

    Depending on the answers

    • If pupils answer 15: ask which variable stores 15, and what is the other variable for.
    • If pupils answer 5: ask on which pass BestPosition was last changed.
    • If pupils are secure: ask them to change the program to find the lowest score and where it is.

    Support

    • Provide the highest-trace table with the Highest column completed.

    Stretch

    • If two players tie for the highest, which position does BestPosition hold, and how could you change that?

    English as an additional language

    • Check highest, position and keeps; use a leaderboard with positions as an example.

    In the lesson window

    • Think it through: The scores typed are 12, 7, 15, 9 and 13. Predict the output. Why does the program need two loops, rather than adding each score to Total as it is typed?
    • Check your understanding: At the end of the program, what does BestPosition hold?
  5. 4. A table of values Discover 13 min

    Purpose: Pupils work out that a cinema seat needs two numbers, then meet two-dimensional arrays, read the plan, and decide what is wrong with a call that gives the indexes the wrong way round.

    Ask

    • How many numbers do you need to pick out Row 2, seat 3?
    • What is Seats[2, 2]? What is Seats[3, 4]?
    • A programmer wants the last seat in row 1 and writes Seats[4, 1]: what is wrong?

    Responses to expect

    • Two
    • F and X
    • The row comes first, so it should be Seats[1, 4]; there is no row 4

    Misconceptions to challenge

    • A learner may believe: “The order of the two indexes does not matter.” Respond: Seats[2, 3] and Seats[3, 2] are different seats, and one may not exist.
    • A learner may believe: “A seat should be found with one number.” Respond: This array has two dimensions, so each element needs two indexes.
    • A learner may believe: “The array should be declared the other way round.” Respond: The declaration matches the plan; the program should read it in the declared order.

    On the board or with the pen

    • Draw the 3 by 4 plan with X in Seats[1,1], Seats[2,3] and Seats[3,4]; write the row numbers down the side and seat numbers across; point to the cell for each question.

    Checking understanding: The seat-order question has four options, three of which are common beliefs (order does not matter, one number, change the declaration).

    Depending on the answers

    • If pupils choose the order does not matter: ask them to find Seats[3, 2] and Seats[2, 3] on the plan and compare.
    • If pupils are secure: ask how many elements ARRAY[1:5, 0:9] has and what its last element is.

    Support

    • Give pupils a labelled grid so they read row then seat with a finger.

    Stretch

    • Declare an array for a timetable of five days and six lessons, and say what the element for Wednesday lesson 4 is.

    English as an additional language

    • Check row and column; use rows of a table in the classroom.

    In the lesson window

    • Think it through: A ticket says “Row 2, seat 3”. How many numbers do you need to pick out one seat? Why would a single number, as in the lockers, be awkward for a seating plan?
    • Apply it: The plan is declared ARRAY[1:3, 1:4]. A programmer wants the last seat in row 1 and writes Seats[4, 1]. What is wrong?
  6. 5. Visiting every seat Explain 15 min

    Purpose: Pupils plan loops to visit every seat, follow a worked count of booked seats, trace it row by row, and decide how many times the inner test is carried out.

    Ask

    • How could loops be arranged to visit all twelve seats once?
    • How many times is the IF line carried out in total?
    • What is Booked after each row?

    Responses to expect

    • An outer loop for rows and an inner loop for seats
    • 12
    • 1, 2, 3

    Misconceptions to challenge

    • A learner may believe: “The line is carried out 7 times, 3 plus 4.” Respond: The inner loop starts again for each row, so the passes multiply.
    • A learner may believe: “The line is carried out 4 times, as the inner loop has 4 passes.” Respond: The inner loop runs 4 times for each of 3 outer passes.
    • A learner may believe: “Booked is reset for each row.” Respond: It is set to 0 once, before both loops.
    • A learner may believe: “Each loop ends with the same NEXT.” Respond: Each loop has its own NEXT, the inner one first.

    On the board or with the pen

    • Write the nested loops and trace the first two rows by marking each cell of the grid with a number from 1 to 8 in the order it is visited; draw an arrow row by row.

    What learners should notice

    • The order of visiting is row 1 seat 1 to 4, then row 2 seat 1 to 4, and so on.

    Checking understanding: The nested-count check: 7 and 4 are the add and the inner-only slips.

    Depending on the answers

    • If pupils answer 7: ask them to count how many seats are checked in row 1 alone, then the whole plan.
    • If pupils trace easily: ask which numbers change in the program if the cinema has 5 rows of 6 seats.

    Support

    • Provide the booked-trace table with the first row filled in.

    Stretch

    • Count the free seats in each row and output the row with the most free seats.

    English as an additional language

    • Check inner and outer; point to a row of seats and then a seat in it.

    In the lesson window

    • Think it through: A single FOR loop counts one number. The plan has rows and seats. How could loops be arranged so that every one of the twelve seats is visited exactly once?
    • Check your understanding: In the counting program, how many times in total is the line IF Seats[Row, Seat] = “X” carried out?
  7. 6. Spot the mistake Apply 6 min

    Purpose: Pupils find why Sam's total stops with an error after the fifth mark: the loop goes one past the upper bound.

    Ask

    • What is the fault and the fix?
    • What is the index on the sixth pass?

    Responses to expect

    • The second loop goes to 6 but the array has no element 6; end it at 5
    • 6

    Misconceptions to challenge

    • A learner may believe: “The first loop should go to 6.” Respond: The array has five elements and the first loop fills all of them; the second loop is the one using 6.
    • A learner may believe: “Total must be an array.” Respond: Total holds one running value.

    On the board or with the pen

    • Draw the five boxes and a sixth dotted box with a question mark; trace Position to 6.

    Checking understanding: The loop-past-end question: the other two options point at the wrong loop and the wrong variable.

    Depending on the answers

    • If pupils choose the first loop: ask which loop uses Marks[6].
    • If pupils fix it quickly: ask them to rewrite so that the bounds appear only once, as a constant.

    Support

    • Highlight both loop headers and ask which has a value the array does not have.

    In the lesson window

    • Apply it: The program stops with an error after adding the fifth mark. What is the fault and the fix?
  8. 7. Write one yourself Apply 14 min

    Purpose: Pupils write a program that reads six values into an array, then totals them and counts the values above 10, including the boundary 10.

    Ask

    • Declare the array, read six values and output the total loans and the number of popular books.
    • What does it output for 14, 3, 11, 10, 22 and 8?

    Responses to expect

    • ARRAY[1:6] OF INTEGER and two loops
    • Total loans: 68 and Popular books: 3

    Misconceptions to challenge

    • A learner may believe: “10 counts as popular.” Respond: Popular is more than 10, so use > 10; 10 is not counted.
    • A learner may believe: “The array holds the total.” Respond: The array holds the six counts; Total is a separate variable set to 0 before the loop.
    • A learner may believe: “The array must be declared inside the loop.” Respond: Declare it once, with its bounds, before the loops.

    On the board or with the pen

    • Draw the array with the six values and a Total and Popular box beside it; update them as the second loop runs.

    Checking understanding: Collect the programs: check the declaration, bounds, input into Borrowed[Book], starting values and the > 10 test.

    Depending on the answers

    • If pupils write >= 10: ask them to test the value 10 and say whether it is more than 10.
    • If pupils finish quickly: ask them to output the position of the most borrowed book too.

    Support

    • Provide the declaration and the first loop, and ask pupils to write the second.

    Stretch

    • Output the average number of loans per book and the number of books above the average.

    In the lesson window

    • Think it through: Write pseudocode that declares the array, reads the six values into it with a loop, and then outputs the total number of loans and the number of popular books. What does it output for the values 14, 3, 11, 10, 22 and 8?
  9. 8. Show what you know Consolidate 7 min

    Purpose: Pupils explain what an array is and when a two-dimensional one is needed, then choose the right indexes for one pupil's total in a two-dimensional array before the summary.

    Ask

    • What is an array, how is it different from separate variables, and when is a two-dimensional one needed?
    • Which loop adds up the three test marks of pupil 2?

    Responses to expect

    • A fixed number of values of one type under one name, found by an index; rows and columns for two-dimensional
    • FOR Test ← 1 TO 3: Total ← Total + Scores[2, Test]

    Misconceptions to challenge

    • A learner may believe: “Scores[Test, 2] adds the marks of pupil 2.” Respond: The first index is the pupil, so the pupil's index must stay at 2 while the test index changes.
    • A learner may believe: “Scores[2, 3] inside the loop adds all three tests.” Respond: Neither index changes, so the same element is added three times.

    Checking understanding: Collect the explanations: look for same type, fixed size, index, and a school example of rows and columns.

    Depending on the answers

    • If pupils choose Scores[Test, 2]: ask which index picks the pupil and which picks the test in this declaration.
    • If several are unsure of two-dimensional arrays: begin the next lesson with the seating plan and ask pupils to read three elements.

    In the lesson window

    • Think it through: Explain to a friend what an array is, how it differs from a set of separate variables, and when a program needs a two-dimensional one. Use a school example of your own.
    • Apply it: Scores is declared ARRAY[1:4, 1:3] OF INTEGER. It holds the three test marks (columns 1 to 3) of four pupils (rows 1 to 4). Which loop adds up the three test marks of pupil 2, with Total starting at 0?
  10. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • An array holds a fixed number of values of one type under one name; each element is found by its index; the declaration gives the bounds.
    • A variable or expression can be an index, so a FOR loop can read or write every element.
    • A two-dimensional array has two indexes, row first, and nested loops visit every element, with the passes multiplying.
    • Store the index when you need to know where, and the value when you need to know what.
    • Usual faults: an index outside the bounds, the row and column the wrong way round, confusing the index with the value.

Consolidation

  • An array holds a fixed number of values of one type under one name; each element is found by its index; the declaration gives the bounds.
  • A variable or expression can be an index, so a FOR loop can read or write every element.
  • A two-dimensional array has two indexes, row first, and nested loops visit every element, with the passes multiplying.
  • Store the index when you need to know where, and the value when you need to know what.
  • Usual faults: an index outside the bounds, the row and column the wrong way round, confusing the index with the value.