7.2.4 Arrays

As many other programming languages, CSI(C&F) offers the possibility to use vectors or multidimensional arrays for comfortable data organization. In this introductional part we will explain the use of arrays by modifying the Fahrenheit-Celsius program as follows:

/* Print Fahrenheit - Celsius table using arrays */

LET Fahreinheit$ = "Fahrenheit : "

LET Celsius$ = "Celsius : "

LET lower = 0

LET upper = 300

LET step = 20

LET fahr = lower

LET index = 0

LOOP

EXITLOOP (fahr>upper)

LET index = index+1

/* the results are stored in an array */

LET result[index].celsius = (5/9)*(fahr-32)

LET result[index].fahr = fahr

LET fahr = fahr+step

ENDLOOP

FOR i = 1 TO index STEP 1

PRINT Fahrenheit$ PRINT result[i].fahr PRINT CR

PRINT Celsius PRINT result[i].celsius PRINT CR

NEXT i

In contrast to other languages like C or C++, arrays in CSI(C&F) do not have to be declared before use; neither has the size of an array to be. Whenever an element of an array is used in an assignment statement like

LET result[index].celsius = (5/9)*(fahr-32)

a new element of this type is created in CSI(C&F)'s data space. When you try to use an arrays element that previously was not created by any assignment statement, CSI(C&F) will generate a warning or an error, depending on the context in which the "unknown" element was incorrectly used.

By the use of '[' and ']' in names of variables and procedures we can easily establish arrays without restrictions on their dimension. In 7.2.5 we will show the use of rectangular multidimensional arrays.

In CSI(C&F), indexing is not only a way of organizing data, but also a concept of names used in assignments, procedures, etc. This includes, that CSI(C&F) provides the feature of constructing pseudo arrays of functions, a concept well known from other programming languages. A brief example will illustrate this concept:

/* pseudo-arrays of procedures */

FOR i = 4 TO 8 STEP 1

LET output = (i>5)

CALL function[output]

NEXT i

EXIT

PROCEDURE function[0]

PRINT i PRINT " is lower than or equal 5"

PRINT CR

RETURN

PROCEDURE function[1]

PRINT i PRINT " is greater than 5"

PRINT CR

RETURN

Depending on the BOOLEAN result of the logical test condition (i>5), the statement

CALL function[output]

alternates in calling function[0] and function[1]. The output of this program would be:

4 is lower than or equal 5

5 is lower than or equal 5

6 is greater than 5

7 is greater than 5

8 is greater than 5