CDC Community

🔹 Historical Conservation 🔹

User Tools

Site Tools


tutor:array_operations

Array Operations

You have seen how to operate on individual elements of an array by using indexed variables. It is also possible to define an array in such a way as to permit operating on the array as a whole. Here are two sets of statements, one using true “arrays” and the other using indexed variables, with both routines calculating the sum of sixty scores (three scores for each of twenty students):

TRUE ARRAY define total=v1 array,scores(3,20)=v31 calc total⇐Sum(scores) INDEXED VARIABLE define total=v1,i-v2,j-v3 scores(a,b)=v(10+20a+b) calc total⇐0 doto 4,i⇐1,3 doto 4,j⇐1,20 calc total⇐total+scores(i,j) 4

The calculation using indexed variables involves initializing “total” to zero, then using nested -doto-s to add in each element of “scores”. The true array calculation is much simpler, involving a single -calc- statement!

The statement “define array,scores(3,20)=v31” tells TUTOR to put scores (1,1) in v31, scores (1,2) in v32, scores (1,3) in v33, etc., with scores (2,1) in v51, scores (2,2) in v52, etc. Moreover, this “array” definition permits you to work with the whole array, and there are various array functions such as “Sum” to help you. The expression “Sum(scores)” means “add up all the numbers in all the elements of the array”. Similarly, the statement “scores⇐scores +1” will cause all sixty array elements to be increased by one.

Such whole-array operations are not possible with indexed variables, because (with indexed variables) TUTOR does not know how many elements make up the whole array. On the other hand, the complexities of handling true arrays limits their size to 255 elements at present and to only two “dimensions” (that is, you can't say “define array,points (2,5,4)=v1”, which would define a three-dimensional array). So, ordinary indexed variables do have their uses, particularly when manipulating large databases (as discussed in the next chapter). While the most useful feature of true arrays is the ability to deal with all elements at once, you can also refer to individual elements, such as scores(2,15), just as you would with indexed variables.

Suppose we define two arrays, A and B, both ten variables long:

define array,A(10)=v141 array,B(10)-v131

The following calculations involving these arrays will have the specified results:

Calculation Result
A⇐2B Each element of A is assigned the value of two times the corresponding element of B: A(1)⇐2B(1),A(2)⇐2B(2), etc.
A⇐25 Each element of A is set to 25
A⇐1/A Each element of A is replaced by its reciprocal
A⇐A+B Corresponding elements of A and B are added together, and the sum replaces the element of A: A(1)⇐A(1)+B(1), A(2)⇐A(2)+B(2), etc.
A⇐2.4cos(B) Presently not allowed: use A⇐B×B instead.
A⇐B $and$ B Each element of A is replaced by –1 or 0, depending on a logical “and” of the corresponding elements of A and B (which should of course contain logical values, -1 and 0, to begin with).

There are a couple of special operators unique to array manipulations: A • B gives the standard “matrix multiplication”, with row-by-column multiplication and summation, and A×B gives the standard “vector product” or “cross product”. If A and B are one-dimensional arrays, the matrix multiplication A • B yields a single number, known in mathematics as the “dot product”. The symbol • is typed by means of MICRO-x, and “x” is typed by MICRO-shift- x .

There are some useful functions:

Sum(A) Adds up all the elements of A
Prod(A) The product of all the elements: A(1)×A(2)…×A(10)
Min(A) Picks out the smallest value
Max(A) Picks out the largest value
And(A) A(1)$and$A(2)$and$A(3)…Sand$A(10)
Or(A) A(1)$or$A(2)$or$A(3)…$or$A(10)
Rev(A) Reverses the order of the elements
Transp(A) Produces the transposed array: A(i,j)⇐A(j,i)

Combinations of the various operations and functions can be used to your advantage. For example, a common statistical calculation involves the square root of the sum of the squares of all array elements. This can be easily obtained from sqrt(Sum(A × A)), or from sqrt(A • A) if A is a one-dimensional array.

Arrays can be filled with a -set- command and displayed with a -showt- command:

define array,C(2,3)-v16 set C⇐100,200,300 200 400 600 400,500,600 will display at 1215 800 1000 1200 showt 2C,5 $$ 5 figures

The -set- command fills elements in order. For example: C(1,1), C(1,2), C(1,3), C(2,1), C(2,2), C(2,3). The -showt- (“show tabular”) command shows the numbers appropriately on the screen. You can also use -showe-, -showo-, and -showa- (but not -show- or -showz- at present).

It is often convenient for the array elements to be offset, so that the first element is not numbered “one”. For example, you might want an array of the world population from 1900 to 1970. In this case, simply say “define array,popul(1900;1970)=v1”, which assigns popul(1900) to vl and popul(1970) to v71. Note the semicolon in the -define-. A twodimensional array with offsets is written “define array,D(-3,0;5,8)= v1”, where D(-3,0) is in vl, D(-3,1) is in v2, etc. The last element of this array is D(5,8).

Integer Variables and Bit Manipulation

tutor/array_operations.txt · Last modified: 2022/08/25 13:51 by site_admin