Arrays can be easily composed and managed in Ada.
You can directly work with them, but a new type will be composed for each instance, if you want to define processes for them, you will need to define a type.
type Year_Temperature is array(integer range 1..365) of integer;type Year_Humidity is array(1..365) of float range 0.0 .. 100.0;type Holidays is array(Days) of Boolean;declare T: Year_Temperature; H: Year_Humidity; My_Week: Holidays := (Sun => True, others => false); typeless_array : array(1..12) of Integer;begin Temperatures(1) := 15;...You can extract slices from arrays, and even assign them as left-values.
T(2..5) := ( 12, 13, 11, 12); T(6..31):= (others => 10); But they have to be of the same size. Even if you assign "the whole array".
Also the operator & concatenates arrays of the same type.
There are some useful attributes on arrays:
for i in integer range T'first .. T'last loop T(i) := 20;end loop;for i in T'range loop T(i) := 20;end loop;But they have to be of the same size. Even if you assign "the whole array".
Also the operator & concatenates arrays of the same type.
You can define an array as multidimensional with two (or more) indexes references.
You can also define arrays of arrays.
type BitMap is array(0..8, 0..8) of Boolean;declare type Bitmap is array(0..15, 0..15) of boolean; icon: Bitmap;begin for x in icon'range(1) loop for y in icon'range(2) loop icon(x, y) := false; end loop; end loop;end;with Ada.Text_IO; use Ada.Text_IO;procedure Hello is type chess_type is (black, white); type Chess_board is array(character range 'a'..'h', integer range 0..8) of Chess_Type; Board: Chess_Board;begin Board('a',7):= White; Put_Line(chess_type'image(board('a',7)));end Hello;If you didn't notice, the indexes can be of different types.
It's also worth noticing that slices only work for uni-dimensional arrays.
What happens if you don't know the size of your array? You're in luck there's a special construct for that.
type Vector is array(integer range<>) of float;These are a little bit deceptive. They don't represent an array which can change in size; they represent an array whose size is not known by the type system. They're implemented via a combination of fat pointers and fixed-size arrays, depending on context. They can be used as a shortcut in declaring variables, but they're mostly useful as parameters.
To implement types of arrays with unknown but fixed length, more like a family of fixed arrays, we can write the following syntax
type <T_Name> is array (integer range<>) of <Type>;note the <> symbol (read box) is part of the code, not an empty variable tag.
Just to make it clear: This types can NOT change size. They are build to be simple and easy for the computer to manage.
Strings are actually of this type.
type Vector is array (integer range <>) of Integer; V1: Vector; -- Doesn't work! Undefined size V2 : Vector(1..6); -- Compiles V3 : Vector := (1,3,2,1); -- Compiles...V3 := (1,2,3); -- NO! Size is 4V3(0..2) := (1,2,3); -- Yes! The default index is 0..Size-1Average( V2 & V3); -- Works, as the concatenation is of the same typeUnknown size array types are common as parameters to procedures and functions.
function average (V: Vector) is Av: Integer := 0;begin for i in V'Range loop Av= Av + V(i); end loop; return (Av/V'Size);Trying to access an unexciting value in the vector would raise a Constrain Error.