% 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} %