DoAssignment study guide
A2.3 · Use nested structures for totals, extrema, and search
Learn to use nested structures for totals, extrema, and search through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Programming Concepts and Skills
ICS3U study topic A2.3
What you will learn
- Explain how a nested structure can organize values and how nested loops visit them.
- Trace updates to a total or greatest or least value as values are visited.
- Use a nested loop and a flag to search nested data for a target.
1. Understand the nested structure and its input
- An outer list can contain inner lists, each holding values.
- The outer loop visits rows; the inner loop visits values within a row.
- Python 3 indexes start at 0; another classroom language may differ.
2. Build a total and find an extreme
- Start a running total at 0 and add each visited value inside the inner loop.
- For a greatest or least value, initialize from an actual item in non-empty data.
- Replace the stored extreme only when the comparison shows a more extreme value.
3. Search nested data and trace the result
- Start a search flag at `False`.
- Compare every visited value with the target inside the inner loop.
- The final flag is `True` if a match was visited and `False` if none was visited.
4. Apply the pattern carefully
- Set up the stored result according to the task before visiting values.
- Keep per-value work inside the inner loop.
- Check the final result against the values and the task.
Trace of the running total and greatest-value candidate
| Task | Value visited | Stored value before | Stored value after |
|---|---|---|---|
| Total | 3 | 0 | 3 |
| Total | 5 | 3 | 8 |
| Total | 2 | 8 | 10 |
| Total | 4 | 10 | 14 |
| Greatest | 8 | 8 | 8 |
| Greatest | 3 | 8 | 8 |
| Greatest | 6 | 8 | 8 |
| Greatest | 11 | 8 | 11 |
Worked example
Total all values
Python 3 row and position indexes are zero-based.
Python 3
values = [[3, 5], [2, 4]]
total = 0
for row in values:
for value in row:
total += value
print(total)- Identify the input and starting stateThe input contains four values in two rows. The code starts `total` at 0, then adds each value as the inner loop visits it.
- Trace the updatesThe visit order is 3, 5, 2, then 4. Add each value to the previous total; the last stored total is the output.
Worked example
Find the greatest value
The nested loops visit each row and position in order.
Python 3
values = [[8, 3], [6, 11]]
largest = values[0][0]
for row in values:
for value in row:
if value > largest:
largest = value
print(largest)- Choose an initial candidateThe input is non-empty, so initialize `largest` from its first value, 8. This ensures the candidate is an actual input value.
- Compare each visited valueThe loop visits 8, 3, 6, and 11. The values 3 and 6 do not replace 8. When 11 is visited, it is greater than the stored candidate, so the candidate becomes 11.
Worked example
Search for a target
The target is at Python row index 1 and position index 0.
Python 3
values = [[12, 7], [9, 15]]
target = 9
found = False
for row in values:
for value in row:
if value == target:
found = True
print(found)- Start with no match recordedThe inputs are the nested list and target 9. Set `found` to `False`, then compare each value visited by the inner loop with the target.
- Record the matchThe first two values, 12 and 7, do not match. The value 9 at row index 1 and position index 0 matches, so `found` changes to `True`. The final value 15 does not change the flag.
Common mistakes and how to avoid them
Lesson summary
- Nested lists can organize values into rows, and nested loops can visit them row by row.
- For a total, start at 0 and add each visited value.
- For a greatest or least value, initialize from an actual item and update after comparison.
- For a search, compare each visited value with the target and track whether any match occurs.
Check your understanding
Question 1
- 8
- 11
- 6
- 5
Show answer and explanation
Question 2
- 4
- 7
- 10
- 27
Show answer and explanation
Question 3
- `False`
- `True`
- 6
- The number of rows
Show answer and explanation
Key terms
- Nested structure
- A structure inside another structure, such as a list of lists or a loop inside another loop.
- Running total
- A stored sum updated as each value is processed.
- Extreme
- The greatest or least value among the values considered.
- Target
- The value a search checks for.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- A2.2 · Use sequence, selection, and repetition
- A3.1 · Use existing subprograms appropriately
- A1.1 · Use variables and constants with appropriate data types
- A1.2 · Explain binary, hexadecimal, and character encoding
- A1.3 · Use arithmetic and string assignment statements
- A1.4 · Use Boolean, comparison, and arithmetic operators correctly
Tutor support for A2.3 · Use nested structures for totals, extrema, and search
Working on A2.3 · Use nested structures for totals, extrema, and search in ICS3U? Bring the question and your own attempt. In a live session, you can trace a small example and find the first step where the output differs from what you expected. Ask about the first step that does not make sense, then try a similar question yourself.
Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.
برای مبحث A2.3 · کاربرد ساختارهای تودرتو برای جمع، بیشینه و جستوجو در درس ICS3U، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A2.3. 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.