Community resourceWorksheet

1CP2-CT-10.2 Traversing displaying and searching two-dimensional data

Part 2 of 5 · 1CP2-CT-10 · Integrated programming and data structures

An outer loop selects each record. Direct field indexing or an inner loop accesses fields inside that record. For a search, compare only the named key field and preserve the complete matched row when the requirement asks for the record.

Students will:

  • traverse records and their fields systematically
  • predict nested traversal before execution
  • search using the named key field
  • construct a search that preserves the complete matched row

Inside: 6 explanation cells, 1 fill-in-the-blanks cell, 2 multiple-choice questions, 4 written answers, 1 trace table and 1 Python task. 19 marks, about 45 minutes.

Series: 1CP2-CT-10 · Integrated programming and data structures, part 2 of 5.

Shared by Coding PathwayVerified teacher

  • 15 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Traversing, displaying and searching two-dimensional data

An outer loop selects each record. Direct field indexing or an inner loop accesses fields inside that record. For a search, compare only the named key field and preserve the complete matched row when the requirement asks for the record.

1. Follow the traversal path

Rows and fields in a two-dimensional structurefield 0field 1field 2field 3row 0row 1row 2AshaRed68YBenBlue73NCaraRed81Yrecords[1][2] → 73

To display each record in columns, use the same field positions for every row. To search by name, compare row[0] with the target. Start a found flag as False; change it only when a match occurs.

Fill in the blanks3 marks
The gap 1 loop moves between records. Access to fields occurs gap 2 each record. A search starts found as gap 3.
  • False
  • inside
  • outer

2. Predict before execution

records = [["Asha", 12], ["Ben", 18], ["Cara", 14]]
for row in records:
    if row[1] >= 14:
        print(row[0])
Multiple choice1 mark

Which names are printed, in order?

  • AAsha only
  • BBen then Cara
  • CCara then Ben
  • DAll three names
Written answer2 marks

Explain why Cara is printed even though her score is not greater than 14.

Refer to the exact operator.

Students type their answer here.

Trace table5 marks

Complete the record-search trace.

Record row, comparisons, found and matched_name.

Use one row for each pass through the loop. Fill in a box only when that value changes on that row, and leave the rest blank.

ProgramPython
  1. records = [["Asha", 12], ["Ben", 18], ["Cara", 14]]
  2. target = "Ben"
  3. found = False
  4. comparisons = 0
  5. matched_name = ""
  6. for row in records:
  7. comparisons = comparisons + 1
  8. if row[0] == target and found == False:
  9. found = True
  10. matched_name = row[0]
  11. print(matched_name)
Trace table with 8 columns
Rowrecordstargetfoundcomparisonsmatched_namerowrow[0] == target and found == FalseOutput
1
2
3
4
5
6
7

Visit every field with nested traversal

When every field must be processed, the outer loop selects each row. An inner loop then selects each field in that row: for row in records: followed by an indented for field in row:. The inner loop completes all fields before the outer loop moves to the next record.

Written answer2 marks

For a 3-row structure with 4 fields in every row, how many times does the inner-loop body execute, and why?

Link rows and fields.

Students type their answer here.

3. Construct a record search

Define find_record(records, target) to return the complete first row whose field 0 equals target, or an empty list if absent. Use linear search; do not sort the records.

Coding task6 marks
records = [["Asha", "Red", 68], ["Ben", "Blue", 73], ["Cara", "Red", 81]]
# Define find_record and use it to find Ben.
Written answer2 marks

Give one found and one not-found test for find_record, with exact expected returned values.

Use the supplied records.

Students type their answer here.

Multiple choice1 mark

Why should this search compare only row[0]?

  • AThe requirement defines field 0 as the name key
  • BOther fields cannot contain data
  • CA loop cannot access field 1
  • DSearching every field sorts the table
Written answer2 marks

The target is in the second of 100 rows. State one refinement that could avoid unnecessary later comparisons and explain its effect.

Stay within PLS-compatible Boolean control.

Students type their answer here.

Route forward

You can traverse, display and search records. Next you will load typed records from a supplied CSV and keep loading, searching and display in focused subprograms.