Parallel Arrays
Altogether now!
Arrays in Parallel¶
-
Until we have full Object-Orientation to work with we need to have data that that is more rich than just one array.
-
One common practice is parallel arrays.
-
With this technique we use more than one array and have the same number of elements in each one. The data in the arrays relates to the data in the other arrays.
-
I think only an example will work here!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
-
Because the arrays are the same length, we can use the
length
of either one of them to run the loop test. -
The magic is that inside the loop the
index
refers to the same position in each array! -
Would a picture help?
This lets us do many neat tricks. Here are some possibilities:¶
- Display the full state name when our data only has the abbreviation.
- Expand a month from 12 to December.
- Associate an inventory item with a stock amount number.
- Search though one of the arrays and output the data from the other. We can convert data with this trick.
Labs
- Lab09: Parallel Arrays
- unit05/labs/lab-09-parallel-arrays.html