| Overall Reading | |
|---|---|
| Brookshear: | p. 321, Ch. 7.1-7.2 (skip second half of p. 326 and all of p. 331) |
Outline:
For example, a program may refer to the array as Readings and then individual cells of the array as Readings[1], Readings[2] and so on.
Converting this conceptual structure to the machine's memory is straightforward.
| 10 | 28 | 13 | 22 |
| 40 | 38 | 11 | 14 |
| 33 | 18 | 25 | 19 |
Though conceptually, this information is two-dimensional, we can store it using a one-dimensional piece of memory.
row major order - Store all entries of the first row, followed by entries in the second row, followed by the third row, etc. On the above example,
| 10 | 28 | 13 | 22 | 40 | 38 | 11 | 14 | 33 | 18 | 25 | 19 |
Based on the number of rows and columns of the table, it is easy to calculate where an entry in the two-dimensional table will be stored in the one-dimensional array.
Notes:
| 10 | 40 | 33 | 28 | 38 | 18 | 13 | 11 | 25 | 22 | 14 | 19 |
If you are in a setting where you do not have advanced knowledge of the amount of data you will be storing, one of two things might happen.
Let's look at an example, such as that of Figure 7.5 (p. 329 of [Br]), however looking at how it might be laid out in memory. For this example, we are told that the "head" of the list begins at cell 11. By conventions, we will designate the end of the list with a pointer value of zero.
| Memory Contents (in decimal) |
Cell | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | Z | 15 | X | 11 | C | 13 | V | 1 | B | 0 | N | 5 | M | 7 | L | 9 |
We can walk through the list, reading the data as follows:
N -> C -> M -> V -> Z -> L -> B
Note: The information currently stored in Cells 3 and 4 is not part
of this list.
| Memory Contents (in decimal) |
Cell | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | Z | 15 | X | 11 | C | 13 | V | 1 | B | 0 | N | 5 | M | 1 | L | 9 |
In our example, lets add "K" to the list so that it is placed between L and B. For storage, we will note that Cells 3 and 4 are available for use. The two pointers we update are as follows. Cell 4 is set to value "9" since the new item "K" will be followed by "B" which is stored in Cell 15. Also, Cell 16 will be set to value "3" so that the entry L in the list is now followed by the new entry "K". The updated list is represented as follows:
| Memory Contents (in decimal) |
Cell | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | Z | 15 | K | 9 | C | 13 | V | 1 | B | 0 | N | 5 | M | 1 | L | 3 |