
Think together · Start with a story
For a week's temperatures, we want the highest value, the average, and the days that got warmer. Arranging them by date in an array keeps each value's position meaningful.
Break down the process
- Organize the valuesKeep the order
- TraverseRead each element
- ProcessCount, compare, or add
- Keep contextRemember which day it was
An array is useful not only for storing many numbers, but also for preserving the meaning of each position.
01Why are arrays so important in algorithms?
Arrays organize a sequence of values so a program can process them with a predictable pattern, often using a loop.
02Problem one: counting
For example, count how many scores are at least 60 by checking each element and increasing a counter when it meets the rule.
03Problem two: searching
To find a particular value, examine elements until you find it or reach the end.
for (int i = 0; i < n; i++) {
if (a[i] == target) {
// found it
}
}
04Problem three: maximum, minimum, and sum
Maximum
Compare values and update as you go.
Minimum
Compare values and update as you go.
Sum
Add values one by one.
These are all basic patterns for traversing an array.
05Problem four: sorting
If you want to arrange an array from smallest to largest, you need a sorting algorithm.
A later “Basic Algorithms” topic will explain why sorting is not just “swapping positions”—it also involves algorithm efficiency.
06Problem five: 2D maps and boards
A two-dimensional array can represent:
Mazes
Record a wall or path in each cell.
Game boards
Record the state of each cell.
Maps
Record the terrain in each cell.
Matrices
Store a number in each cell.
07What more advanced ideas grow from arrays?
You will encounter ideas such as:
- Prefix sums
- Binary search
- Sorting
- Two pointers
- State tables in dynamic programming
- Adjacency structures in graph algorithms
Many advanced algorithms still come back to the question of how to organize and access a group of data.
08What matters most when learning arrays?
What you have learned
- Arrays are a core data structure for learning algorithms.
- Counting, searching, summing, and finding maximum or minimum values can all use array traversal.
- A two-dimensional array can represent a grid, board, or matrix.
- Advanced algorithms such as sorting, binary search, and prefix sums also rely heavily on arrays.
- Focus on how data is organized and accessed, rather than memorizing templates.
Topic complete: Next, explore “Functions and Program Structure.”
Your turn to try
If you sort the temperatures to find the highest one, can you still treat the first position as Monday?
Ready? Open to see the explanation
No. Sorting changes the positions. To know which day had the highest temperature, keep the dates or record the original index.