aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/pages/ARRAY1.ht
blob: c2199a05f4ff2239faa8c5ee6d2e0a897901225a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\OneDimensionalArrayXmpTitle}{OneDimensionalArray}
\newcommand{\OneDimensionalArrayXmpNumber}{9.57}
%
% =====================================================================
\begin{page}{OneDimensionalArrayXmpPage}{9.57 OneDimensionalArray}
% =====================================================================
\beginscroll
The \spadtype{OneDimensionalArray} domain is used for storing data in a
one-dimensional indexed data structure.
Such an array is a homogeneous data structure in that all the entries of
the array must belong to the same \Language{} domain.
Each array has a fixed length specified by the user and arrays are not
extensible.
The indexing of one-dimensional arrays is one-based.
This means that the ``first'' element of an array is given the index
\spad{1}.
See also \downlink{`Vector'}{VectorXmpPage}\ignore{Vector} and \downlink{`FlexibleArray'}{FlexibleArrayXmpPage}\ignore{FlexibleArray}.
\xtc{
To create a one-dimensional array, apply the
operation \spadfun{oneDimensionalArray} to a list.
}{
\spadpaste{oneDimensionalArray [i**2 for i in 1..10]}
}
\xtc{
Another approach is to first create \spad{a}, a one-dimensional array of 10 \spad{0}'s.
\spadtype{OneDimensionalArray} has the convenient abbreviation \spadtype{ARRAY1}.
}{
\spadpaste{a : ARRAY1 INT := new(10,0)\bound{a}}
}
\xtc{
Set each \spad{i}th element to i, then display the result.
}{
\spadpaste{for i in 1..10 repeat a.i := i; a\bound{a1}\free{a}}
}
\xtc{
Square each element by mapping the function
\texht{$i \mapsto i^2$}{i +-> i**2} onto each element.
}{
\spadpaste{map!(i +-> i ** 2,a); a\bound{a3}\free{a2}}
}
\xtc{
Reverse the elements in place.
}{
\spadpaste{reverse! a\bound{a4}\free{a3}}
}
\xtc{
Swap the \spad{4}th and \spad{5}th element.
}{
\spadpaste{swap!(a,4,5); a\bound{a5}\free{a4}}
}
\xtc{
Sort the elements in place.
}{
\spadpaste{sort! a \bound{a6}\free{a5}}
}
\xtc{
Create a new one-dimensional array \spad{b} containing the last 5 elements of \spad{a}.
}{
\spadpaste{b := a(6..10)\bound{b}\free{a6}}
}
\xtc{
Replace the first 5 elements of \spad{a} with those of \spad{b}.
}{
\spadpaste{copyInto!(a,b,1)\free{b}}
}

\endscroll
\autobuttons
\end{page}
%