Creating an Array: Difference between revisions

m
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:
<lang algol68>REF []INT numbers = HEAP [1:10] INT;
<lang algol>
HEAP [1:10]INT sugared hnumbers; # from global memory #
REF []INT numbers = HEAP [1:10] INT;
HEAPLOC [1:10]INT sugared hnumberslnumbers1; # from globalthe memorystack #
LOC [1:10]INT sugared lnumbers1lnumbers2; # from the stack - LOC scope is implied #</lang>
[10]INT sugared lnumbers2; # from the stack - LOC scope is implied #
</lang>
Note that the array can be taken from the heap, or stack.
 
Example of array of 3 string types:
<lang algol68>[3]STRING vwordscwords := ( "these", "are", "arrays" ); # array is a variableconstant and modifiableread-only (:=) #
<lang algol>
[3]STRING cwordsvwords := ( "these", "are", "arrays" ); # array is a constantvariable and read-onlymodifiable (:=) #</lang>
YouThe coder can also declare the size of the array and initialize the values at the same time:
[3]STRING vwords := ( "these", "are", "arrays" ); # array is a variable and modifiable (:=) #
<lang algol68>REF []INT more numbers = LOC [3]INT := ( 21, 14 ,63 );
</lang>
[]INT sugared more cnumbers = ( 21, 14 ,63 );
You can also declare the size of the array and initialize the values at the same time:
[3]INT sugared more vnumbers := ( 21, 14 ,63 );</lang>
<lang algol>
For Multi-Dimensional arrays youthe coder declare them the same except for a comma in the type declaration.
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 );
</lang>
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;
<lang algol>
REF [][3,2]INT sugared number matrix1; # an matrix =of LOCintegers [3][2] INT;#
[3,][2]INT sugared number matrix1matrix2; # an matrixarray of arrays of integers #</lang>
As with the previous examples youthe coder can also initialize the values of the array, the only
[3][2]INT sugared number matrix2; # an array of arrays of integers #
</lang>
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.
<lang algol68>[][]STRING string matrix = ( ("I","swam"), ("in","the"), ("freezing","water") );</lang>
<lang algol>
[][]STRING string matrix = ( ("I","swam"), ("in","the"), ("freezing","water") );
</lang>
or
<lang algol68>REF [2][2] STRING sugared funny matrix = LOC [2][2]STRING := ( ("clowns", "are") , ("not", "funny") );
<lang algol>
REF [2][2] STRING sugared funny matrix = LOC [2][2]STRING := ( ("clowns", "are") , ("not", "funny") );</lang>
[2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") );
</lang>
Further, the arrays can start at any number:
<lang algol68>[-10:10] INT balanced; # for example -10 #</lang>
<lang algol>
[-10:10] INT balanced; # for example -10 #
</lang>
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>
<lang algol>
FLEX [-10:10] INT flex balanced;
</lang>
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;
<lang algol>
[-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>
</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 #
<lang algol>
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>
</lang>
Initialization by an aggregate using positional and keyed notations:
<lang algolalgol68>VEC x := (1, 4, 5);
VEC x := (1, 4, 5);
[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>
</lang>
 
==[[AmigaE]]==