Introducing arrays
As programs become more complex,
we notice that they require more variables,
and thus more variable names to hold all necessary values.
We could define:
int x1, x2, x3, x4, x5..... ;
but referring to them in our programs will quickly become unwieldy,
and their actual names may be trying to tell us something.
In particular,
our variables are often related to one another -
they hold data having a physical significance, and the data value held in one
variable is related to the data in another variable.
For example, consider a 2-dimensional field,
where each square metre of the field may be identified by its
rows and column coordinates.
We may record each square's altitude, or temperature, or its number of ants:
(0,0) |
(1,0) |
(2,0) |
(3,0) |
(0,1) |
(1,1) |
(2,1) |
(3,1) |
(0,2) |
(1,2) |
(2,2) |
(3,2) |
Like most languages, C provides a simple data structure,
termed an array,
to store and access data where the data items themselves are closely related.
Depending on the context,
different problem domains will describe different kinds of arrays with
different names:
- 1-dimensional arrays are often termed vectors,
- 2-dimensional arrays are often termed matrices
(as in our example, above),
- 3-dimensional arrays are often termed volumes,
and so on.
We'll start with the simple 1-dimensional arrays.
CITS2002 Systems Programming, Lecture 5, p1, 5th August 2024.
|