Array: Difference between revisions

1,848 bytes added ,  16 years ago
Multidimensional arrays added, operations on arrays added. Definitions extended and fixed
(Pascal allows any lower index)
(Multidimensional arrays added, operations on arrays added. Definitions extended and fixed)
Line 1:
[[Category:Encyclopedia]]An '''array''' is a composite data type, in the [[Collections|collection]] category, that stores multiple values all of the same declared type,. indexThe accessedstored byvalues aare numericcalled [[array'''elements''' index]]and whichare isaccessed computedby ata [[runtuple time]]of indices. By using a variable indexindices, the array may be accessed for a not predetermined set of indices, during the run of the program. AnAll arrayindices inof whichan indicesarray are not[http://en.wikipedia.org/wiki/Total_order numerictotally is known as an [[associative array]ordered].
 
An array of which some of the indices have infinite [http://en.wikipedia.org/wiki/Upper_and_lower_bounds bounded subsets] is known as an [[associative array]]. For example, arrays indexed by [http://en.wikipedia.org/wiki/Lexicographical_order lexicographically ordered] strings are associative.
The array has a lower bound, and an upper bound, beyond which it is illegal to access the array. The lower bound is in many [[:Category:Programming Languages|programming languages]] fixed to either 0 ([[C]] and relatives) or 1 (Old [[Fortran]] and relatives), or arbitrary ([[Pascal]] and relatives, modern Fortran) but the upper bound is always programmable. The size of the array is the distance from the lower bound to the upper bound and one extra. In all regular programming languages, the size of the array can be determined by the programmer at [[compile time]] or after. In many modern programming languages the size of the array may be computed and allocated at run time. In some programming languages, the lower bound may also be specified by the programmer.
 
Arrays with more than one index are called '''multidimensional''' arrays. For example, matrix is a two-dimensional array.
All values between (and including) the lower and the upper bound of the array may legally be accessed during the program run.
 
Basic operations defined on arrays are:
Array programming languages supply special support for arrays. In them most actions apply to entire arrays, so programs in such languages often lack specific index reference.
* Indexing, accessing an array element by its tuple of indices;
* Slicing, producing a subarray by putting some constraint on the indices. For example, [[PL/1]] provides extracting of a rows or a columns of an array. In [[Ada]] any range of the index can be used in order to extract a subarray from a single-dimensional array;
* Iteration of the arrays elements. Some languages have [[Loop/Foreach]] construct for array iteration;
* Querying the bounds of array indices;
* Operations on indices (next, previous, range etc);
* Array programming languages supply special support for arrays. In them mostprovide actionsoperation applyapplied to entire arrays, so programs in such languages often lack specific index reference.
 
Values of arrays are called '''array aggregates'''.
 
Multidimensional arrays with some of indices varying are called '''ragged'''. This term comes from a typical example of a ragged array, when a two-dimensional array is used to store strings of different length in its rows. When put on paper the right margin of the output become ''ragged''.
 
Each array index is bounded at run time. Further when
 
# array is not associative
# each index is within the bounds (including the bounds)
 
then it is legal to access the array at the tuple of these indices.
 
The lower bound is in many [[:Category:Programming Languages|programming languages]] fixed to either 0 ([[C]] and relatives) or 1 (Old [[Fortran]] and relatives), or arbitrary ([[Pascal]] and relatives, modern Fortran). In [[Ada]] any discrete type can used as an index.
 
For an empty array the lower bound of an index is greater than the upper bound. This causes the famous problem of declaring an empty array when the index type is a singleton (has only one value).
 
When bounds of all array indices are fixed, the array is said to be '''constrained'''. When some bounds are unknown until run time, the array is '''unconstrained'''. Non-associative unconstrained arrays are usually passed to the subprograms with a "dope" attached to the array body. The dope contains the actual bounds of unconstrained array indices. Associative arrays carry some indexing structure with them, like a [[hash table]] etc.
 
The minimal size of a non-associative array is determined by the current bounds of its indices. In all regular programming languages, the size of such array can be set by the programmer at [[compile time]] or after. In many modern programming languages the size of the array may be computed and allocated at run time.
 
In most programming languages, arrays are accessed by using the array brackets <code>[</code> and <code>]</code>, e.g. in <code>A[i]</code>, but exceptions are [[Rexx]], instead using the dot operator <code>.</code>, such as in <code>A.i</code>, [[Fortran]], [[Ada]] and [[BASIC]] which use round parentheses <code>A(i)</code>, and of course in the [[LISP|lisps]] which uses constructions like <code>(ELT A n)</code> for accessing and <code>(SETA A n new_val)</code> for setting (Interlisp) or <code>(vector-ref A n)</code> for accessing and <code>(vector-set! A n new_val)</code> for setting (Scheme). No bracket indexing occurs in [[J]], an array language; instead, the normal syntax of function creation and function calling applies.