Multi-dimensional array: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 21:
All Ada arrays are instances of some array type. The array type may be explicitly declared, or if undeclared is said to be an anonymous type.
 
{{'''Ranges}}'''
 
Ada array indices employ the concept of a range. A range is specified as Low_Value .. High_Value. The range is inclusive of both the low value and the high value.
<lang Ada>
Line 40 ⟶ 41:
Deflection is defined as a distinct integer type with a minimum value of -180 and a maximum value of 180.
 
{'''Constrained vs Unconstrained}'''
 
Ada array types may be constrained types or unconstrained types. A constrained type is defined with a constrained index range. An unconstrained type is defined with an unconstrained index range. All instances of an unconstrained array type are themselves constrained.
{{Single dimensional arrays}}
Line 56 ⟶ 58:
</lang>
The instance of string identified as Name above is created and initialized to hold the value "Rosetta Code". The first index value for Name is 1. The last index value for Name is 12.
 
{{'''Array Attributes}}'''
 
Every array has a set of attributes which are implemented as implicitly defined functions.
 
Line 79 ⟶ 83:
 
''''Size''' This attribute returns the size in bits of the array in memory.
 
{{'''Arrays of Arrays}}'''
 
Ada allows the definition of an array type which is any array of another array type.
<lang Ada>
type Vector is array (1..10) of Integer;
type MatrixTable is array (0..99) of Vector;
</lang>
In this example, one can access the second element of an instance of type Vector using the notation
Line 91 ⟶ 97:
V(2) := 10;
</lang>
Accessing the second element of the vector on the second row of MatrixTable is done as
<lang Ada>
T : Table;
...
T(1)(2) := 10;
</lang>
Each element of Table is an instance of Vector. Each element of Vector is an Integer.
 
'''Multi-Dimensional Arrays'''
 
Ada also allows the definition of multi-dimensional arrays.
<lang Ada>
type Matrix is array (0..99, 1..10) of Integer;
M : Matrix;
...
M(1)(,2) := 10;
</lang>
The example above shows how to access the element in the second row and second column of an instance of Matrix.
 
Whole array instances can be assigned to other arrays of the same type.
<lang Ada>
Mat1 : Matrix;
Mat2 : Matrix;
...
Mat1 := Mat2;
</lang>
The above assignment creates a deep copy. After the assignment Mat1 and Mat2 are separate instances of Matrix containing the same values.
 
Slices of arrays can also be assigned to instances of the same array type such as
<lang Ada>
V1 : Vector := (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
V2 : Vector := (2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
...
V2(3..6) := V1(7..10);
</lang>
When assigning whole arrays or slices of arrays the sizes must match, but the index ranges can be different. In the example above each slice contains 4 data elements. The value of V2 after the assignment is (2, 2, 7, 8, 9, 10, 2, 2, 2, 2).
 
'''Looping through Arrays'''
 
The Ada "for" loop is used to iterate through arrays without danger of index bounds violations
<lang Ada>
function "*" (Left : Vector; Right : Integer) return Vector is
Result : Vector;
begin
for I in Vector'Range loop
Result(I) := Left(I) * Right;
end loop;
return Result;
end "*"
</lang>
The function definition above overloads the multiplication operator "*" allowing the multiplication of every element of and instance of Vector by an Integer value. The "for" loop within the function iterates over each value of the range produced using the 'Range attribute. Unlike some languages such as C or C++, no additional parameter specifying the number of elements in the array is passed. This approach works well for constrained array types. A small variation is used for unconstrained array types.
<lang Ada>
type Unconstrained_Vector is array (Positive range <>) of Integer;
U1 : Unconstrained_Vector := (1,2,3,4,5,6,7,8,9,10);
U2 : Unconstrained_Vector := (10,11,12,13);
...
function "*" (Left : Unconstrained_Vector; Right : Integer) return Unconstrained_Vector is
Result : Unconstrained_Vector(Left'Range);
begin
for I in Left'Range loop
Result(I) := Left(I) * Right;
end loop;
return Result;
end "*";
</lang>
The instance of Unconstrained_Vector defined within the function is initialized to have the same index values as the formal parameter named Left. This version of the function allows the programmer to pass any instance of Unconstrained_Vector to the function "*". In the example above one may pass both U1 and U2 to the function without experiencing index boundary violation.
 
=={{header|ALGOL 68}}==
82

edits