A1.5 · Use one-dimensional arrays of compound data
Learn to use one-dimensional arrays of compound data through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Programming Concepts and Skills
ICS4U A1.5 study lesson
In earlier programming, you may have stored several values of one kind in an array, such as a list of temperatures. In this lesson, each array item groups related information about one thing. For example, one item can hold a learner’s name and score. The array is still one-dimensional: its items are arranged in a single sequence, and each item can be reached by an index. The focus here is using that structure clearly and accurately.
What you will learn
Explain how a one-dimensional array can store compound data.
Identify the fields that make up each item and use an index to access an item.
Trace a short program that reads, processes, or updates compound data in an array.
Choose a suitable array structure for a small set of related records.
1. From a simple array to compound data
A one-dimensional array is an ordered sequence of items. Each item has an index, which tells you its position. In Python 3, a list is commonly used for this purpose, and its first index is 0. The list itself can contain compound items rather than individual numbers or strings.
Compound data means that one item groups multiple related values. A student record, for instance, might contain a name and a score. Those values describe the same student, so keeping them together helps prevent a name from becoming separated from the score it belongs to. The array holds several such records, one after another.
For this lesson, each record is represented as a dictionary with named fields. A field name makes the role of a value visible: `record['name']` is the name, and `record['score']` is the score. Python dictionaries are one way to represent compound data; the important idea is that each array item holds a related group of values.
The array has one sequence of indexed items.
Each item groups related values into a record.
A field name identifies the role of a value within a record.
2. Read, trace, and update records
Before working with an array of records, identify what each record contains and what the program is expected to do. For example, if every record has a `name` field and a `score` field, the input must provide both values for each student. A program that displays names should read the `name` field from each record; it should not treat the whole record as if it were a string.
A reliable way to follow such a program is to trace one array item at a time. Note the current index, the record at that index, and the field being read or changed. If a loop visits every index from the first item to the last, each record is considered once. When changing a field, the array still contains the same record at that index, but its field value has been updated.
The short example below assumes that `students` is already a list of dictionaries, and that each dictionary contains the fields `name` and `score`. Its input is that list. The steps are to visit records in order and display each record’s name. The expected output is the names in the same order as the records. This is a code reading example; the displayed code is not represented as having been run.
State the record fields and input before tracing.
Use an index to select a record, then a field name to select a value.
The order of output follows the order of the array when records are visited in sequence.
3. Keep the representation consistent
A record structure is useful only when the program uses it consistently. If one record uses `score` and another uses `mark`, code that expects `score` may not work for both. Decide on field names and keep them the same throughout the array. Also make sure that each record has the fields needed by the task.
Think of the array index and the field name as answering different questions. The index answers, “Which record?” The field name answers, “Which piece of information in that record?” For example, `students[1]['name']` selects the name field of the record at index 1. This two-part access is the central operation in this topic.
When planning a solution, write down the record’s fields, the input records, and the required result. Then trace a small sample by hand. This makes it easier to spot an incorrect index, an inconsistent field name, or a mistaken assumption about the output before relying on the code.
Use consistent field names across records.
Select a record by index and a value within it by field name.
A hand trace can confirm that the code matches the stated input and task.
4. Build confidence with complete examples
The examples use small lists so that every record can be followed by hand. In each case, the precondition states what the input contains, the code shows the algorithm, and the trace gives the expected result. The diagrams display the array as a single indexed sequence; the fields shown inside each cell belong to that cell’s record.
Python lists are used as the one-dimensional array representation in these examples. In a different classroom language, the syntax or record representation may differ, but the key task remains to keep related values together and access the intended field of the intended item.
Check the array contents before tracing.
Follow the stated algorithm in index order.
Compare the expected output with the values selected from the records.
Worked example
Display names from records
Precondition and input: `students` is a list of dictionaries, each with a `name` and a `score`. The list contains Mira with score 84, then Oren with score 91. Visit each record and display its name.
Students as a one-dimensional array
Each cell is one record; the index selects the record and the field name selects a value.
Python 3
students = [
{'name': 'Mira', 'score': 84},
{'name': 'Oren', 'score': 91}
]
for i in range(len(students)):
print(students[i]['name'])
Python 3 code
The code assumes the stated list and field names. Its algorithm visits indexes in order, selects the record at each index, and prints that record’s name. The expected output contains Mira followed by Oren.
Trace
At index 0, the record’s name is Mira, so Mira is displayed. At index 1, the record’s name is Oren, so Oren is displayed. No score is displayed because the code reads only the name field.
Answer: Expected output: Mira, then Oren, each on its own line.
Check: The order and names match the two records, and the score field is not used.
Worked example
Find the highest score and its student
Precondition and input: `results` is a non-empty list of records. Each record has `name` and `score` fields. The records are Hana with 76, Dev with 89, and Luis with 83. Determine the name and score of the highest-scoring student.
Records examined in order
The current highest record changes when a larger score is found.
Python 3
results = [
{'name': 'Hana', 'score': 76},
{'name': 'Dev', 'score': 89},
{'name': 'Luis', 'score': 83}
]
highest = results[0]
for i in range(1, len(results)):
if results[i]['score'] > highest['score']:
highest = results[i]
print(highest['name'], highest['score'])
Python 3 code
The algorithm starts by treating the first record as the current highest. It then checks each later record. When a score is greater, it saves that whole record as the current highest, keeping the name and score together. The non-empty input precondition ensures that a first record exists.
Trace
Start with Hana at 76. Dev’s score of 89 is greater, so Dev becomes the current highest. Luis’s score of 83 is not greater than 89, so the saved record stays Dev. The expected result is Dev and 89.
Answer: The highest score is 89, belonging to Dev.
Check: The comparison uses each record’s score, while saving the record preserves the matching name.
Worked example
Update one field in a selected record
Precondition and input: `inventory` is a list of records, each with `item` and `quantity`. It contains notebooks with quantity 12 and folders with quantity 7. Increase the quantity of folders by 3, leaving other records unchanged.
Inventory before and after the update
The folders record is selected by its field value; its quantity changes from 7 to 10.
Python 3
inventory = [
{'item': 'notebooks', 'quantity': 12},
{'item': 'folders', 'quantity': 7}
]
for i in range(len(inventory)):
if inventory[i]['item'] == 'folders':
inventory[i]['quantity'] += 3
print(inventory)
Python 3 code
The code assumes the item named folders appears in the list exactly once. It visits records in order, checks each `item` field, and changes only the matching record’s `quantity` field by adding 3. The expected final quantities are 12 for notebooks and 10 for folders.
Trace
At index 0, notebooks does not match folders, so its quantity stays 12. At index 1, folders matches; its quantity changes from 7 to 10. The array still has two records, and only the requested field is updated.
Answer: After the update: notebooks have quantity 12; folders have quantity 10.
Check: The code changes the quantity field only in the record whose item field matches folders.
Common mistakes and how to avoid them
Treating a whole record as though it were the value of one field.
Correction: Select the record with its index, then use the field name to access the specific value needed.
Using different field names for the same information in different records.
Correction: Choose one field name, such as `score`, and use it consistently in every record and in the code.
Updating a record’s score or quantity but losing track of which item it belongs to.
Correction: Keep the related values together in the record and update the chosen field through that record.
Assuming the first record exists when the input might be empty.
Correction: State and check the non-empty precondition when an algorithm starts from the first record.
Lesson summary
A one-dimensional array can contain records, with each record grouping related values.
Use the array index to choose a record and a field name to choose a value within it.
Consistent record fields make reading, comparing, and updating compound data clear.
Trace records in order and verify the expected output against the stated input.
Check your understanding
Question 1
A list has a record at index 2 with fields `name` and `score`. Which access selects that record’s score?
records['score'][2]
records[2]['score']
records[2]
records['name'][2]
Show answer and explanation
records[2]['score']
The index selects the record at position 2, and the field name selects its score.
Question 2
A program visits records in order and prints each record’s `name`. The names are Imani, Bo, and Talia at indexes 0, 1, and 2. What is the output order?
Talia, Bo, Imani
Imani, Bo, Talia
Only the name at index 0
The names and scores mixed together
Show answer and explanation
Imani, Bo, Talia
The loop visits indexes in order, and it prints only the name field from each record.
Question 3
A record has fields `item` and `quantity`. To add 4 to the quantity of the matching item, which approach keeps the update focused?
Add 4 to every field in every record.
Find the record whose `item` matches, then add 4 to its `quantity` field.
Replace the array index with the quantity.
Change the `item` field to the new quantity.
Show answer and explanation
Find the record whose `item` matches, then add 4 to its `quantity` field.
The matching item identifies the record, and updating its quantity changes only the intended information.
Key terms
One-dimensional array
An ordered sequence of items that can be selected by index.
Compound data
Related values grouped together as one item, such as a name and score in one record.
Record
One grouped item of compound data, containing named fields.
Field
A named value within a record.
Index
A position used to select an item in an array; Python lists begin at index 0.
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A1.5. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.