Creating an Array: Difference between revisions

Content added Content deleted
Line 40: Line 40:


==[[ALGOL 68]]==
==[[ALGOL 68]]==
As with all ALGOL 68 declarations the programmer has the choice of using the full declaration syntax, or using [[syntactic sugar]] - c.f. "sugared" variables below.
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:
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;
HEAP [1:10]INT sugared hnumbers; # from global memory #
LOC [1:10]INT sugared lnumbers1; # from the stack #
LOC [1:10]INT sugared lnumbers1; # from the stack #
[10]INT sugared lnumbers2; # 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.
Note that the array can be taken from the heap, or stack.


Example of array of 3 string types:
Example of array of 3 string types:
<lang algol68>[]STRING cwords = ( "these", "are", "arrays" ); # array is a constant and read-only (=) #
<lang algol>
[]STRING cwords = ( "these", "are", "arrays" ); # array is a constant and read-only (=) #
[3]STRING vwords := ( "these", "are", "arrays" ); # array is a variable and modifiable (:=) #</lang>
The 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 the 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
The following creates a 3x2 int matrix
<lang algol68>REF [][]INT number matrix = LOC [3][2] INT;
<lang algol>
REF [][]INT number matrix = LOC [3][2] INT;
[3,2]INT sugared number matrix1; # an matrix of integers #
[3,2]INT sugared number matrix1; # an matrix of integers #
[3][2]INT sugared number matrix2; # an array of arrays of integers #</lang>
As with the previous examples the 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.
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
or
<lang algol68>REF [][] STRING funny matrix = LOC [2][2]STRING := ( ("clowns", "are") , ("not", "funny") );
<lang algol>
REF [][] STRING funny matrix = LOC [2][2]STRING := ( ("clowns", "are") , ("not", "funny") );
[2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") );</lang>
[2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") );
</lang>
Further, the arrays can start at any number:
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.
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
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.
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
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 #
array of pointers to ints[index] := HEAP INT := i*i # allocate global memory #
OD
OD</lang>
</lang>


{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
Line 101: Line 83:


{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
{{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 MAT = FLEX[0,0]REAL;
# MODE STRING = FLEX[0]CHAR; builtin #
# MODE STRING = FLEX[0]CHAR; builtin #
MODE BOOLS = FLEX[0]BOOL; # BITS is builtin in the standard #
MODE BOOLS = FLEX[0]BOOL; # BITS is builtin in the standard #</lang>
</lang>
Initialization by an aggregate using positional and keyed notations:
Initialization by an aggregate using positional and keyed notations:
<lang algol>
<lang algol68>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;
[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));
MAT e := ((1, 0), (0, 1));
Line 117: Line 96:
[2]BOOL b := (TRUE, TRUE);
[2]BOOL b := (TRUE, TRUE);
BITS byte := (TRUE, TRUE);
BITS byte := (TRUE, TRUE);
SKIP
SKIP</lang>
</lang>


==[[AmigaE]]==
==[[AmigaE]]==