CDC Community

🔹 Historical Conservation 🔹

User Tools

Site Tools


Additional Calculation Topics

Before discussing additional TUTOR calculational capabilities, let's review briefly those aspects which have been covered so far:

  1. Expressions follow the rules of high school algebra. Multiplication takes precedence over division, which takes precedence over addition and subtraction. Superscripts may be used to raise numbers to powers. The symbol -a may be used to mean 3.14159 The degree sign (°) may be used to convert between degrees and radians.
  2. There are 150 student variables, vl through v150, which may be named with the -define- command. These variables can be set or altered by assignment (⇦) and by -store-, -storen-, or -storea- commands. If a “define student” set of definitions is provided, the student may use variable names in his or her responses.
  3. Logical expressions are composed using the operators =, >, <, ≥, ≤, $and$, $or$, and the “not” function. Logical expressions have the value true (-1) or false (0).
  4. There are several available system variables such as “where”, “wherey”, “anscnt”, “jcount”, “spell”, etc. Available system functions include sin(x), sqrt(x), etc. A full list of system variables and functions is given in Appendix C.
  5. The -show- command (and its relatives -showt-, -showz-, -showe-, and -showo-) will display the numerical value of an expression. The -showa- command will display stored alphanumeric information. These commands may be embedded within -write- and -writec- statements.
  6. The -calcc- and -calcs- commands make it easy to perform (conditionally) one of a list of calculations or assignments.
  7. The -randu- command with one argument picks a fraction between 0 and 1. With two arguinents, it picks an integer between 1 and the limit specified. There is a set of commands associated with permutations: -setperm-, -randp-, -remove-, and -modperm-.
  8. The iterative form of the -do- command facilitates repetitive operations.

Now let's look at additional TUTOR calculational capabilities.

Defining Your Own Functions

While many important functions such as In(x) and log(x) are built-in to the TUTOR language, it is frequently convenient to define your own functions. To take a simple example, suppose you define a cotangent function:

define cotan(a)=cos(a)/sin(a)

Then, later in your lesson you can write:

calc r⇦cotan(3x+y-5)

and TUTOR will treat this as though you had written:

calc r⇦[cos(3x+y-5)/sin(3x+y-5)]

Such use of functions not only saves typing but improves readability.

CAUTION: In defining a function, the arguments must not be already defined. For example, the following definition will be rejected by TUTOR (with a suitable error message):

define x=v1 cube(x)=x3

This must be rewritten as:

define x=v1 cube(dummy)=dummy3

or anything similar. A function definition may involve previously defined quantities on the right side of the “=” sign, however. You might have:

define x=v1 new(c)=c4+2x

In this case you might have a -calc- that looks like:

calc x⇦15.7 y⇦3new(8)

and this would be equivalent to:

calc x⇦15.7 y⇦3[(8)4+2x

Sometimes it is convenient to define “functions” that have no arguments:

define r=v1 quad=r2-100 r3=r1/3 root=sqrt(r) prod=r3xroot trans=(r⇦prod)

Note that “prod” depends on two previous definitions, each of which (in turn) depends on the definition of “r”. There is no limit on how deep you can go in definition levels. The unusual definition of “trans” permits you to write an unusual -calc- (where the assignment is implicit in the definition of “trans”):

calc trans

Essentially anything is a legal definition. The only rule is that the definition make sense when enclosed in parentheses (since a defined name when encountered in an expression is replaced by its meaning and surrounded by parentheses). This means that you cannot define “minus=-” because (-), a minus sign enclosed in parentheses, is not permitted in an expression. On the other hand, “minus=-1” is all right because (-1) is meaningful.

A function may have up to six arguments. Here is a function of two arguments:

define modulo(N,base)=N-[base×int(N/base)]

This means that modulo (17,5) in an expression will have the value 2; the “int” or “integral part” function throws away the fractional part of 17/5, leaving 3, so that we have (17-5×3)=(17-15)=2. This modulo function, therefore, gives you what is left over in division of “N” by “base”.

Here are a couple of other examples of multi-argument function definitions:

define big(a,b)=-[a×(a-≥b)+b×(b>a)] small(a,b)=-[a×(a≤b)+b×(b<a)]

The minus sign appears because logical true is represented by -1. If you have “big(x+y,z)” in an expression, with (x+y)=7 and z=3, this expands to:


which reduces to -[7×(-1)+3×(0)] which is 7. So our “big” function picks out the larger of two arguments.


It is often important to be able to deal with arrays of data such as a list of exam scores, the number of Americans in each 5-year age group together with their corresponding mortality and fertility rates, a list of which pieces are where on a chess board, or the present positions of each of several molecules in the simulation of the motion of a gas.

Suppose we have somehow entered the exam scores for twenty students into variables v31, v32, v33 . . . up to v50. Here is a unit which will let you see the score of the 5th or 13th or Nth student:

unit see back index at 1215 write Which student number? (Press BACK when done.) arrow 1518 store N wrongv 10.5,9.5 $$ range 1 to 20 write The score of the ⦓s,N⦔th student is ⦓s,v(30+N)⦔

(The -wrongv- rather than -ansv- makes it easy to ask another question.) The new element here is the “indexed variable”:


which means “evaluate 30+N, round to the nearest integer, and choose the corresponding variable”. For example, if N is 9, v(30+N) is v(39) or v39. If N is 13.7, v(30+N) means v44.

We might list and total all the scores:

calc total 0 $$ initialization step do showem,N⇦1,20 at 3035 write The average score is ⦓s,total/20⦔. * unit showem at 835+100N show v(30+N) calc total⇦total+v(30+N)

As usual, it is preferable to define a name for this data, such as:

define scores(i)=v(30+i)

in which case we would write our last unit as:

unit showem at 835+100N show scores(N) calc total⇦total+scores(N)

Due to the special meaning attached to “v(expression)” you must exercise some care in using a variable named “v”, in that you must write “v×(a+3b)” and not “v(a+3b)” if you mean multiplication. We will see later that the same restriction applies to the names “n”, “vc”, and “nc”. This restriction does not apply to students entering algebraic responses, where “v(a+3b)” is taken to mean “v×(a+3b)”. Students can use indexed variables only if they are named (as in “scores” in the above example). Such definitions must, of course, be in the “define student” set.

Suppose you have three sets of exam scores for the twenty students. This might conveniently be thought of as a 3 by 20 (“two-dimensional”) array. Suppose we put the first twenty scores in v31 through v50, the second set in v51 through v70, and the third set in v71 through v90. It might be convenient to redefine your array in the following manner:

define scores(a,b)=v(10+20a+b)

Then, if you want the 2nd test score for the 13th student, you just refer to scores (2,13) which is equivalent to v(10+40+13) or v(63). If you wanted to display all the scores you might use “nested” -do- statements:

do column,i⇦1,3 * unit column do rows,j⇦1,20 * unit rows at 820+10i+100j show scores(i,j)

Unit “column” is done three times and for each of these iterations, unit “rows” is performed twenty times.

There is an alternative way to define our array:

define i=v1,j=v2 scores=v(10+20i+j)

Then our unit “rows” would look like:

unit rows at 820+10i+100j show scores

The indices specifying which test is for which student are implicit. This form is particularly useful when you have large subroutines where “i” and “j” are fixed and it would be tiresome to type over and over again “scores(i,j)”. Just set “i” and “j”, then -do- the subroutine.

It is frequently necessary to initialize an entire array to zero. One way to do this is with -do- statements:

unit clear do clear2,i⇦1,3 * unit clear2 do clear3,j⇦1,20 * unit clear3 calc scores(i,j)⇦O

A simpler way to accomplish the same task is to say:

zero scores(1,1),60

You simply give the starting location (the first of the 60 variables) and the number of variables to be cleared to zero. As another example, you can clear all of your variables by saying:

zero v1,150

Not only is the -zero- command simpler to use, but TUTOR can carry out the operation several hundred times faster! TUTOR keeps a block of its own variables, each of which always contains zero. When you ask for 150 variables to be cleared, TUTOR does a rapid block transfer of 150 of its zeroed variables into your specified area. This ultra-high-speed block transfer capability can be used in other ways. For example:

transfr v10;v85;25

performs a block transfer of the 25 variables starting with v10 to the 25 variables starting with v85. In this way you can move an entire array from one place to another with one -transfr- command, and at speeds hundreds of times faster than are possible by other means.

Segmented Variables

tutor/calculations_2.txt · Last modified: 2022/08/25 21:06 by site_admin