Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Pascal allows any lower index)
Line 1: 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, index accessed by a numeric [[array index]] which is computed at [[run time]]. By using a variable index, the array may be accessed for a not predetermined set of indices, during the run of the program. An array in which indices are not numeric is known as an [[associative array]].
[[Category:Encyclopedia]]An '''array''' is a composite data type, in the [[Collections|collection]] category, that stores multiple values all of the same declared type, index accessed by a numeric [[array index]] which is computed at [[run time]]. By using a variable index, the array may be accessed for a not predetermined set of indices, during the run of the program. An array in which indices are not numeric is known as an [[associative array]].


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 ([[Pascal]] and relatives), 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.
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.


All values between (and including) the lower and the upper bound of the array may legally be accessed during the program run.
All values between (and including) the lower and the upper bound of the array may legally be accessed during the program run.

Revision as of 20:04, 9 May 2008

An array is a composite data type, in the collection category, that stores multiple values all of the same declared type, index accessed by a numeric array index which is computed at run time. By using a variable index, the array may be accessed for a not predetermined set of indices, during the run of the program. An array in which indices are not numeric is known as an associative array.

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 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.

All values between (and including) the lower and the upper bound of the array may legally be accessed during the program run.

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.

In most programming languages, arrays are accessed by using the array brackets [ and ], e.g. in A[i], but exceptions are Rexx, instead using the dot operator ., such as in A.i, Fortran, Ada and BASIC which use round parentheses A(i), and of course in the lisps which uses 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.
)

Computational metrics

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