Anonymous user
Creating an Array: Difference between revisions
m
→[[Creating an Array#ALGOL 68]]: fix lang tag
m (→J) |
m (→[[Creating an Array#ALGOL 68]]: fix lang tag) |
||
Line 40:
==[[ALGOL 68]]==
As with all ALGOL 68 declarations the programmer has the choice of using the full declaration syntax, or using [[wp:syntactic sugar|syntactic sugar]] - c.f. "sugared" variables below.
Example of array of 10 integer types:
HEAP [1:10]INT sugared hnumbers; # from global memory #
▲ REF []INT numbers = HEAP [1:10] INT;
Note that the array can be taken from the heap, or stack.
Example of array of 3 string types:
▲ [3]STRING vwords := ( "these", "are", "arrays" ); # array is a variable and modifiable (:=) #
▲You can also declare the size of the array and initialize the values at the same time:
For Multi-Dimensional arrays
▲ REF []INT more numbers = LOC [3]INT := ( 21, 14 ,63 );
▲ []INT sugared more cnumbers = ( 21, 14 ,63 );
▲ [3]INT sugared more vnumbers := ( 21, 14 ,63 );
▲For Multi-Dimensional arrays you declare them the same except for a comma in the type declaration.
The following creates a 3x2 int matrix
<lang algol68>REF [][]INT number matrix = LOC [3][2] INT;
▲As with the previous examples you can also initialize the values of the array, the only
difference being each row in the matrix must be enclosed in its own braces.
▲ [][]STRING string matrix = ( ("I","swam"), ("in","the"), ("freezing","water") );
or
▲ [2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") );
Further, the arrays can start at any number:
<lang algol68>[-10:10] INT balanced; # for example -10 #</lang>▼
▲[-10:10] INT balanced; # for example -10 #
If the array is expected to change size in future, then the programmer can also declare it FLEX.
<lang algol68>FLEX [-10:10] INT flex balanced;</lang>▼
▲FLEX [-10:10] INT flex balanced;
This next piece of code creates an array of references to integers. The value of
each integer "pointed at" is the square of it's index in the array.
<lang algol68>[-10:10] REF INT array of pointers to ints;▼
▲[-10:10] REF INT array of pointers to ints;
FOR index FROM LWB array of pointers to ints TO UPB array of pointers to ints DO
array of pointers to ints[index] := HEAP INT := i*i # allocate global memory #
OD</lang>
{{works with|ALGOL 68|Standard - no extensions to language used}}
Line 101 ⟶ 83:
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>MODE VEC = FLEX[0]REAL; # VECTOR is builtin in ALGOL 68R #▼
▲MODE VEC = FLEX[0]REAL; # VECTOR is builtin in ALGOL 68R #
MODE MAT = FLEX[0,0]REAL;
# MODE STRING = FLEX[0]CHAR; builtin #
MODE BOOLS = FLEX[0]BOOL; # BITS is builtin in the standard #</lang>
Initialization by an aggregate using positional and keyed notations:
<lang
[100]INT y; FOR i TO UPB y DO y[i]:=0 OD; FOR i FROM 5 TO 20 DO y[i]:= 2 OD; y[2]:=y[3]:= 1;
MAT e := ((1, 0), (0, 1));
Line 117 ⟶ 96:
[2]BOOL b := (TRUE, TRUE);
BITS byte := (TRUE, TRUE);
SKIP</lang>
==[[AmigaE]]==
|