% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved. % !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk. \newcommand{\TwoDimensionalArrayXmpTitle}{TwoDimensionalArray} \newcommand{\TwoDimensionalArrayXmpNumber}{9.82} % % ===================================================================== \begin{page}{TwoDimensionalArrayXmpPage}{9.82 TwoDimensionalArray} % ===================================================================== \beginscroll The \spadtype{TwoDimensionalArray} domain is used for storing data in a %-% \HDindex{array!two-dimensional}{TwoDimensionalArrayXmpPage}{9.82}{TwoDimensionalArray} \twodim{} data structure indexed by row and by column. Such an array is a homogeneous data structure in that all the entries of the array must belong to the same \Language{} domain (although see \downlink{``\ugTypesAnyNoneTitle''}{ugTypesAnyNonePage} in Section \ugTypesAnyNoneNumber\ignore{ugTypesAnyNone}). Each array has a fixed number of rows and columns specified by the user and arrays are not extensible. In \Language{}, the indexing of two-dimensional arrays is one-based. This means that both the ``first'' row of an array and the ``first'' column of an array are given the index \spad{1}. Thus, the entry in the upper left corner of an array is in position \spad{(1,1)}. The operation \spadfunFrom{new}{TwoDimensionalArray} creates an array with a specified number of rows and columns and fills the components of that array with a specified entry. The arguments of this operation specify the number of rows, the number of columns, and the entry. \xtc{ This creates a five-by-four array of integers, all of whose entries are zero. }{ \spadpaste{arr : ARRAY2 INT := new(5,4,0) \bound{arr}} } The entries of this array can be set to other integers using the operation \spadfunFrom{setelt}{TwoDimensionalArray}. \xtc{ Issue this to set the element in the upper left corner of this array to \spad{17}. }{ \spadpaste{setelt(arr,1,1,17) \free{arr}\bound{arr1}} } \xtc{ Now the first element of the array is \spad{17.} }{ \spadpaste{arr \free{arr1}} } \xtc{ Likewise, elements of an array are extracted using the operation \spadfunFrom{elt}{TwoDimensionalArray}. }{ \spadpaste{elt(arr,1,1) \free{arr1}} } \xtc{ Another way to use these two operations is as follows. This sets the element in position \spad{(3,2)} of the array to \spad{15}. }{ \spadpaste{arr(3,2) := 15 \free{arr1}\bound{arr2}} } \xtc{ This extracts the element in position \spad{(3,2)} of the array. }{ \spadpaste{arr(3,2) \free{arr2}} } The operations \spadfunFrom{elt}{TwoDimensionalArray} and \spadfunFrom{setelt}{TwoDimensionalArray} come equipped with an error check which verifies that the indices are in the proper ranges. For example, the above array has five rows and four columns, so if you ask for the entry in position \spad{(6,2)} with \spad{arr(6,2)} \Language{} displays an error message. If there is no need for an error check, you can call the operations \spadfunFrom{qelt}{TwoDimensionalArray} and \spadfunFromX{qsetelt}{TwoDimensionalArray} which provide the same functionality but without the error check. Typically, these operations are called in well-tested programs. \xtc{ The operations \spadfunFrom{row}{TwoDimensionalArray} and \spadfunFrom{column}{TwoDimensionalArray} extract rows and columns, respectively, and return objects of \spadtype{OneDimensionalArray} with the same underlying element type. }{ \spadpaste{row(arr,1) \free{arr2}} } \xtc{ }{ \spadpaste{column(arr,1) \free{arr2}} } \xtc{ You can determine the dimensions of an array by calling the operations \spadfunFrom{nrows}{TwoDimensionalArray} and \spadfunFrom{ncols}{TwoDimensionalArray}, which return the number of rows and columns, respectively. }{ \spadpaste{nrows(arr) \free{arr2}} } \xtc{ }{ \spadpaste{ncols(arr) \free{arr2}} } \xtc{ To apply an operation to every element of an array, use \spadfunFrom{map}{TwoDimensionalArray}. This creates a new array. This expression negates every element. }{ \spadpaste{map(-,arr) \free{arr2}} } \xtc{ This creates an array where all the elements are doubled. }{ \spadpaste{map((x +-> x + x),arr) \free{arr2}} } \xtc{ To change the array destructively, use \spadfunFromX{map}{TwoDimensionalArray} instead of \spadfunFrom{map}{TwoDimensionalArray}. If you need to make a copy of any array, use \spadfunFrom{copy}{TwoDimensionalArray}. }{ \spadpaste{arrc := copy(arr) \bound{arrc}\free{arr2}} } \xtc{ }{ \spadpaste{map!(-,arrc) \free{arrc}} } \xtc{ }{ \spadpaste{arrc \free{arrc}} } \xtc{ }{ \spadpaste{arr \free{arr2}} } \xtc{ Use \spadfunFrom{member?}{TwoDimensionalArray} to see if a given element is in an array. }{ \spadpaste{member?(17,arr) \free{arr2}} } \xtc{ }{ \spadpaste{member?(10317,arr) \free{arr2}} } \xtc{ To see how many times an element appears in an array, use \spadfunFrom{count}{TwoDimensionalArray}. }{ \spadpaste{count(17,arr) \free{arr2}} } \xtc{ }{ \spadpaste{count(0,arr) \free{arr2}} } For more information about the operations available for \spadtype{TwoDimensionalArray}, issue \spadcmd{)show TwoDimensionalArray}. For information on related topics, see \downlink{`Matrix'}{MatrixXmpPage}\ignore{Matrix} and \downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray}. \endscroll \autobuttons \end{page} %