Previous Section
Cover Page
Table of Contents
Index
Exercises for this Section
Next Section

Section 8 - Arrays

It is assumed that the reader is familiar with the use of arrays and their associated commands on the keyboard and various menus. An array may be a vector or a matrix. These are discussed in chapters 9, 10 and 11 of UG. Besides the GET and PUT commands to access specific elements of an array, there is an algebraic way of accessing them that is frequently much more convenient in programs. In what follows let v be a vector and M be a matrix. Then 'v(k) + 7' EVAL will get the kth element of v, add 7 to it, and leave the result on the stack. The sequence 12 'M(2,5)' STO will replace the element in the 2nd row and 5th column of M with the number 12. We will also be making use of several memory arithmetic commands in what follows. These commands are not described anywhere in UG so they are explained below. These commands can be found in LS PRG F2-MEM F6-ARITH.

The first four are STO+, STO-, STOx, and STO/. They require the name of a variable to be on either level 1 or level 2 of the stack and a constant on the other of level 1 or level 2. When one of these four commands is executed the contents of the variable replaces the variable name on the stack, the requested arithmetic is performed (if the values are compatible), and the result is removed from the stack and stored in the named variable. For example, suppose the vector [1, 3] is on level 2 of the stack and the variable 'A', which contains a 4, is on level 1. After executing STOx, the variable A will contain the vector [4, 12]. These four commands work with any combination of real numbers, complex numbers, and arrays so long as the requested arithmetic is defined. STO+ also works for concatenation of lists and strings.

The fifth and sixth commands are INCR (increment) and DECR (decrement). These commands require the name of a variable on level 1 of the stack. For most applications the variable will contain an integer. Executing one of these commands will remove the name from the stack, increment or decrement the variable by 1 and leave the new value on level 1 of the stack.

On the second page of this menu are three more commands, SINV (inverse), SNEG (negative), and SCONJ (conjugate). They require the name of a variable on level 1 of the stack. Executing one of the commands will remove the name from the stack and take the inverse, negative, or conjugate of the contents of the variable.

Suppose that A is a vector of unknown length that contains integers and that has been stored in memory. We want to find the average of the elements of A to one decimal place and then determine if one of the elements of A is equal to that average. The SIZE command can be found at LS MTH F2-MATRX F1-MAKE F6-SIZE and GET is LS MTH F2-MATRX F1-MAKE NXT F1-GET.

	<<
	   A SIZE 1 GET 1 0 0  n k sum av
	   <<
	      1 n FOR j
	         'sum' 'A(j)' EVAL STO+
	      NEXT
	      sum n / 'av' STO 1
	      WHILE n  1 FC? AND REPEAT
	         IF 'A(k)==av' THEN
	            1 SF k
	         ELSE
	            'k' INCR
	         END
	      END
	      "Average = " 1 FIX av + ".  It is " + STD
	      IF 1 FS?C THEN
	         "The " + k R->I +
	         CASE
	            k 1 == THEN "st" END
	            k 2 == THEN "nd" END
	            k 3 == THEN "rd" END
	            "th"
	         END
	         + "  element of A."
	      ELSE
	         "NOT IN A."
	      END
	      + MSGBOX
	   >>
	>>

The command STD, found in LS PRG NXT F4-MODES F1-FMT F1-STD, puts the display back to standard. Save this program as FDAV and single step through it with several different vectors A to see how it works. Be sure to choose values that will test all the paths through the IF and CASE structures.

EXERCISE SET 8

1. With a vector A of unknown length as in the example FDAV above, write a program, FDSM, to go through the vector to find the smallest value and to see if there is more than one element with that smallest value. The output should include the smallest value, the index of the first occurrence of the smallest value, and a statement about whether or not that value is unique.

Previous Section
Cover Page
Table of Contents
Index
Top of Page
Next Section