Contents - Index


Array Range Notation

 

Array range notation is a shorthand notation to assign array variables or to facilitate passing of array variables to built-in, internal and external Functions and Procedures.  A range of array variables can be indicated by separating the first array index value from the last index value by two decimal points.  For example, X[1..5] can be used in place of X[1], X[2], X[3], X[4], X[5] as the argument list to a function.  This shorthand notation is supported for two dimensional array variables as well, e.g., Z[2,1..3].  EES variables may be used in the index range (e.g., X[N..M]) provided that their values have been previously set in the Equations window or in the Parametric table. The notation can be used in the arguments of function calls and CALL statements, in Function and Procedure statements and in $Common directives.

 

Array range notation can be used in assigning values to an array in the following manner:

 

N=3

A[1..N]=[33,X,Y^2]

 

The above statement is a short-hand (and equivalent) alternative to entering

A[1]=33

A[2]=X

A[3]=Y^2

 

Starting with EES version 11.488, Units may be assigned to array variables using array name notation.  For  example, the following statement sets the values of array rho and assigns each element units of kJkg.

 

rho[1..5]=[33,21,42,51,13] [kJ/kg]

 

Note that EES will expect the designated number of values (5 in the example above) to exist within the brackets on the right-side of the assignment.  An error will posted if this is not the case.  If more values are provided than required, a warning will be posted.

 

Two-dimensional array information can also be entered in this short-hand notation provided that only one dimension is entered at a time, e.g.,

 

B[1..3,2]=[12,22,33]  "Short-hand for B[1,2]=12;  B[2,2]=22;  B[3,2]=33"

  

Array range notation can be convenient when array variables are being used as arguments to internal and external functions.  However, the major purpose of this notation is to allow long argument lists to be passed.  A maximum of 2000 values (total for all arguments) may be passed to an internal Function or procedure using array range notation.  

 

Array range notation can be used with variable limits, provided that the variable is previously specified.  For example, X[1..n] can be used if n is set to a value before the X[1..n] is encountered.  When Array range notation is used in the header statement of an internal Function or Procedure, a problem occurs since the value of variable, cannot possibly be known at the time the routine is compiled.  In this case, EES provides a default maximum dimension of 100.  If you wish to have a larger maximum, you can specifically provide a number for the maximum or explicitly set the maximum by providing a $DefaultArraySize directive in the Equations window. This capability is only applicable for one-dimensional arrays.  The calling program can provide any number of elements for the array provided it is less than the maximum.  

 

The following example should help describe this capability.

 

$TabStops 2 in

 Function SumSquares(n, A[1..n])

   S:=0

   i:=1

   repeat

      S=S+A[i]^2

       i:=i+1

   until (i>n)

  SumSquares:=S

end

 

"Note than n elements in array X are passed to array A in Function SumSquares.  n must be less than or equal to the maximum number of elements for which array A is dimensioned which is 100 by default."

 

n=90 "note that this program will not work if n is set to a value greater than 100"

 "To make it work for larger values of n, change the Function statement to indicate the maximum dimension"

 "for array A, e.g., Function SumSquares(n,A[1..200]).  Alternatively use a $DefaultArraySize directive.

 

duplicate i=1,n  "initialize n elements array elements"

    X[i]=i

end

SumX2=SumSquares(n, X[1..n])   "returns the sum of the squares of n array elements." 

 

AvgX=AVERAGE(X[1..n])  "AVERAGE is a  built-in function.  It can accept up to 2000 arguments."

SumX=SUM(X[1..n])   "SUM is a built-in function.  It accepts a list of variables with array name notation."

OldSumX=SUM(X[i],i=1,n)   "This is the old notation for the SUM function;  it is still supported."

MaxX=MAX(X[1n])   "MIN and MAX functions accept a variable number of arguments."