Array: Difference between revisions

From Rosetta Code
Content added Content deleted
(+ Pascal)
Line 97: Line 97:
begin
begin
{ set pixel (4,7) to yellow }
{ set pixel (4,7) to yellow }
pixel[4, 7, red] := 255;
picture[4, 7, red] := 255;
pixel[4, 7, green] := 255;
picture[4, 7, green] := 255;
pixel[4, 7, blue] := 0
picture[4, 7, blue] := 0
end.
end.
</pascal>
</pascal>

Revision as of 10:16, 27 October 2008

An array is a composite data type, meaning it can store multiple values, and so is in the collection category. The stored values are called elements of the array and are accessed by a sequence of indices. In a program, indices may be a mixture of constant values or variables which allow the elements accessed to vary under program control. The indices of an array are totally ordered.


The implementation details of an array gives rise to an important distinction between arrays and associative arrays.

The implementation of arrays is based on setting the bounds of indices of the array, the size of the array, allocating a contiguous region of memory to hold the elements of the array, and using simple offset calculations of the indices from the origin of the memory to access memory elements. An extension is to allow such arrays to be resized, or re-shaped, in which the memory area is adjusted, but common elements are kept.
In an associative array more complex hash functions are used to encode the indices of the array and sophisticated hash lookup algorithms are used to map indices to their corresponding elements. The number and range of indices is not pre-set and element storage in extendible so the storage of associative arrays can grow as more indices are used. The hash functions of associative arrays usually allow types other than ranges of integers or fixed enumerations to be used as indices. A common feature is to allow arbitrary strings as indices.
Non-associative arrays have speed and memory consumption advantages. Associative arrays have greater flexibility in types used for indexing and the range of indices.


Arrays with more than one index are called multidimensional arrays. For example, a matrix is a two-dimensional array.

Common operations defined on arrays include:

  • Indexing: accessing an array element by its indices. (There is a one to one mapping between an index and its corresponding element).
  • Slicing: producing a subarray by putting some constraint on the indices. For example, PL/1 provides extracting of a row or a column 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 over the array's elements. Some languages have a foreach loop construct for array iteration.
  • Iteration over the indices of an associative array.
  • Querying the bounds of array indices.
  • Querying the indices of an associative array.
  • Operations on indices (next, previous, range etc)
  • Array programming languages provide operations applied to entire arrays, so programs in such languages often lack specific index reference

Multidimensional arrays in which the valid range of one index depends on the value of another are called ragged (also jagged). 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.

The lower bound of non-associative arrays in many programming languages is commonly fixed at either 0 (C and relatives) or 1 (Old Fortran and relatives); or an arbitrary integer (Pascal and relatives, modern Fortran). In Ada any discrete type can used as an index.

In most programming languages, arrays are accessed by using the array brackets [ and ], e.g. in A[i], but exceptions exist, including Rexx which instead uses the dot operator ., such as in A.i; Fortran, Ada and BASIC which use round parentheses A(i), and in lisp dialects which use constructions like (ELT A n) for accessing and (SETA A n new_val) for setting (Interlisp) or (vector-ref A n) for accessing and (vector-set! A n new_val) for setting (Scheme). No bracket indexing occurs in J, an array language; instead, the normal syntax of function creation and function calling applies.

C

We wish to open a text file and compute letter frequency

FILE *any_text;
/* declare array */
int frequency[26]; 
/* declare a computed index */
int ch; 
 
any_text = fopen ("a_text_file.txt", "rt");
 
/* init the freq table: */
for (ch = 0; ch < 26; ch++) 
    frequency[ch] = 0;
 
ch = fgetc(any_text);
while (!feof(any_text)) {
    if (is_a_letter(ch))
        /* if the char is a letter, then increase character slot in the freq table: */
        frequency[ch-'A'] += 1;
    ch = fgetc(any_text);
}

J

The example task is the same: open a text file and compute letter frequency.
Written in this array programming language, no loops are specified.
Input is a directory-path with filename. Output is a 26-element single-axis integer array.

load 'files'     NB. fread is among these standard file utilities
ltrfreq=: 3 : 0
 letters=. (65+(i.26)) { a.                  NB. We'll work with minimal alphabet.
 reduced=. (#~ e.&letters) toupper fread y   NB. Omit non-letters. (y is the input.)
 sums   =. +/"_1 = reduced                   NB. Count how often each letter occurs.
 sums (letters I. (~. reduced))} 26 # 0      NB. Alphabetize the sums, then return.
)

OCaml

same task, open a text file and compute letter frequency

<ocaml>let () =

 let ic = open_in Sys.argv.(1) in
 let base = int_of_char 'a' in
 let arr = Array.make 26 0 in
 try while true do
   let c = Char.lowercase(input_char ic) in
   let ndx = int_of_char c - base in
   if ndx < 26 && ndx >= 0 then
     arr.(ndx) <- succ arr.(ndx)
 done
 with End_of_file ->
   close_in ic;
   for i=0 to 25 do
     Printf.printf "%c -> %d\n" (char_of_int(i + base)) arr.(i)
   done</ocaml>

Here is the documentation of the module Array. (there is also a Bigarray module)

Pascal

This defines an array suitable to hold a 64x64 truecolor image (i.e. red, green and blue RGB values all can go from 0 to 255) and then sets the color of a single pixel <pascal> type

 color = red, green, blue;
 rgbvalue = 0 .. 255;

var

 picture: array[0 .. 63, 0 .. 63, color] of rgbvalue

begin

 { set pixel (4,7) to yellow }
 picture[4, 7, red]   := 255;
 picture[4, 7, green] := 255;
 picture[4, 7, blue]  := 0

end. </pascal>

Computational metrics

Access is O(1), appending is O(1), and insertion is O(n).