Sequential Processing of Arrays
Sequential access processes array elements one after another in array order, while random access lets you reach any element directly at any time.
A Surprising Question About Birthdays
Suppose you walk into a room and start asking people for their birthdays. How long do you expect to wait before someone shares a birthday with a person you already asked? Most people give an answer that is much larger than what actually happens. That gap between expectation and reality is the starting point for this article. It also introduces one of the most useful properties of arrays, random access, and shows how a simple array lets a program answer the question quickly.
To answer that kind of question in code, a program has to keep track of what it has already seen. The way an array does this, and the reason the access pattern matters, is the subject of the rest of this article. We will look at two ways of visiting the elements of an array, and then trace a birthday simulation step by step to see which one it needs.
Visiting Elements One After Another
Up to this point, the array processing examples in the course have used sequential access. In sequential access, the elements of an array are processed one after another, in the order they appear in the array. You start at the first position, move to the second, then the third, and so on until you finish. Many useful tasks look like this, such as totaling all the values in a list or scanning for the largest one.
Sequential access processes array elements one after another in array order. The program moves from one position to the next position in the array, beginning at the first position and ending when the array has been fully processed.
Sequential access describes how the work is ordered, not a limit on what the array can do. Whenever a problem asks you to visit elements in order, sequential processing is the natural fit.
The phrase describes the shape of the work. A program that totals a list visits every position once, in order, and it has no need to skip around. The same array could also be used in a different way, and that different way is random access.
Reaching Any Slot Through Its Index
Random access means that every element of the array is equally accessible at any given time. A program does not need to walk through the earlier positions to reach a later one. If it wants the element at a particular position, it can go straight to that position, whether the position is the first, the middle, or the last.
One of the big advantages of arrays is that they allow random access. Because every slot can be reached by its index, reading the element at position 2 costs the same kind of step as reading the element at position 0 or at the last position. Nothing in the array forces you to pass through the slots in between. The diagram below shows a small array with five positions. Each cell can be reached by its index, and the program can read one cell without inspecting any other cell.
The figure is a small illustration with five positions instead of 365. To check day number 2, the program names used[2] and reads it. It does not need to inspect used[0] or used[1] first. Those two cells hold true and false respectively, but they have no bearing on the lookup of position 2. This direct reach is what makes random access useful when the next value to examine could be anywhere in the array.
Tracing a Lookup to Position 2
Reading one slot without touching the others
The array used has five positions, indexed 0 through 4. Its contents are true, false, false, true, false. A program needs to know whether day number 2 has already been seen. How many slots must it read?
- 1Identify the index
The question is about day number 2, and the index is the day number, so the index needed is 2.
- 2Name the slot
The program writes used[2] to refer to the slot at index 2 directly.
- 3Read the slot
The value stored there is false, so day 2 has not been seen yet.
- 4Ignore the other slots
used[0] holds true and used[1] holds false, but neither affects the answer for position 2. The program never had to look at them.
Exactly one slot, used[2], was read, and the answer is false. Random access made the lookup a single direct step.
Why the Birthday Simulation Needs a Record
To simulate the birthday experiment, the program must remember which birthdays it has already seen. There are 365 possible birthdays, and ignoring leap years is part of the setup. For each possible birthday, the program needs one piece of information: has someone with this birthday already been found? The answer is a boolean value. Gathering 365 boolean answers into one structure is exactly what an array is for.
In the birthday simulation, the array is a boolean array of 365 slots. The index is the day number, and the value in used[i] is true if someone whose birthday is day number i has already been selected, and false otherwise.
The index does the work of identifying the day. The program never searches the array for a matching birthday. It jumps straight to the slot that corresponds to the birthday it just drew.
Starting From Unseen and Recording a Birthday
Before you look at the answer, make a prediction. The array is created fresh, and no birthdays have been recorded at all. What will the program find in each slot before any simulation step has run, and what should it do when the first birthday arrives?
Predict what the program finds when day number 2 is drawn for the very first time. Which slot is read, what value does it hold, and what does the program write back?
Hints
- Think about the starting value of every slot in a new boolean array.
- The index for the drawn birthday is the day number itself.
New boolean arrays start with false in every slot, so unseen birthdays need no setup before they are checked. When day 2 is drawn, the program reads used[2], finds false, and records the day as used by writing true into that same slot. The diagram shows the state after that first draw: only the slot at index 2 has changed.
Checking a Repeated Birthday
Each time the simulation picks a person, the birthday it draws could be any day from 0 to 364. The next birthday has no relationship to the previous one, so the program cannot know in advance which slot it will need. A purely sequential scan would waste effort walking past slots that have nothing to do with the new birthday. Random access solves this neatly: the program computes the day number, reads used at that index, and either stops or records the day as used.
A second draw of the same birthday
Starting from the state where only used[2] is true, a second person is picked and has day number 2. What does the program do?
- 1Compute the index
The drawn birthday is day 2, so the index is 2. No search through the array is needed.
- 2Read the slot
The program reads used[2] and finds true, meaning someone with this birthday has already been selected.
- 3Decide
Because the slot is already true, the program has found a shared birthday. The check ends here for this trial.
The repeated birthday is detected from a single read of used[2]. Had the next draw been day 4 instead, the program would have read used[4], found false, and written true there.
Sequential Scanning Versus Direct Jumps
The two access patterns answer different kinds of questions. Sequential access suits tasks where every element must be visited in order, such as totaling values or scanning for a maximum. Random access suits tasks where the next element to examine depends on information that arrives while the program runs. The birthday problem is of the second kind. The next day number is not known in advance, so a sequential scan from position 0 would have to walk past many unrelated slots before reaching the right one.
Mistakes Beginners Make With Array Access
Common mistake Searching the array for a matching birthday instead of indexing it
What it looks likeLooping over all 365 slots to find whether day 2 appears somewhere in the array.
Why it failsThe index is the day number, so the program can jump straight to the slot. A search adds unnecessary steps.
Do this insteadCompute the day number and read used at that index directly.
Common mistake Assuming random access means the array is unordered
What it looks likeBelieving that because you can jump to any slot, the positions have no meaning.
Why it failsSequential access describes how work is ordered, not a limit on what the array can do. The positions still have a fixed order.
Do this insteadUse sequential processing for tasks that visit elements in order, and use direct indexing when the next element depends on the data.
Common mistake Doing setup before checking unseen birthdays
What it looks likeWriting extra code to set every slot to false before the simulation starts.
Why it failsNew boolean arrays already start with false in every slot, so unseen birthdays need no setup before they are checked.
Do this insteadCreate the boolean array and begin checking slots directly.
Common mistake Thinking a far slot costs more to reach than a near one
What it looks likeAssuming used[364] is harder to read than used[0].
Why it failsEvery element of an array is equally accessible at any given time, so the position does not change how the program reaches it.
Do this insteadRead any slot by its index. Only sequential scanning makes earlier positions matter.
Tracing a Few Draws on Your Own
Start with a new used array of five positions, all false. Draw the days 4, then 1, then 4 again. For each draw, state which slot is read, what it holds, and what the program writes. On which draw does the program detect a repeated birthday?
Hints
- Each draw uses its day number as the index, with no searching.
- A slot only needs to be written after the program has confirmed it was false.
Key Takeaways
- Sequential access processes array elements one after another in array order, which suits tasks such as totaling values or scanning for a maximum.
- Random access means every element of an array is equally accessible at any given time, so a program can jump straight to the slot it needs by its index.
- The birthday simulation uses a boolean array of 365 slots, where the index is the day number and the value records whether that birthday has been seen.
- New boolean arrays start with false in every slot, so unseen birthdays can be checked immediately without setup.
- The next birthday can point to any slot, which is why random access, not a sequential scan, is the natural way to record and check values in this simulation.