The Ada Program: sub.adb

  1 -- sub.adb:  array attributes, operations, and slices
  2 
  3 with Ada.Text_IO, Ada.Integer_Text_IO;
  4 use Ada;
  5 
  6 procedure Sub is
  7 
  8    type Int_Array_Type is array (Integer range <>) of Integer;
  9 
 10    -- put last half of B at the beginning of B
 11    procedure Mix (B: in out Int_Array_Type) is
 12       -- Pivot is in the range B'Range EXCEPT when B has null range
 13       Pivot: constant Integer := B'First + (B'Length / 2);
 14    begin
 15       B := B(Pivot..B'Last) & B(B'First..Pivot-1);
 16    end Mix;
 17 
 18    subtype A_Range is Integer range -3..11;
 19    subtype A_Type is Int_Array_Type (A_Range);
 20 
 21    -- Initialize array "A" with just some arbitrary values;
 22    -- notice that A(1) is skipped
 23    A: A_Type := A_Type'(-3=>1, -2=>2, -1=>3, 0=>5, 2=>7, others=>0);
 24 
 25 begin
 26 
 27    Mix (A(-2..5));  -- a slice with 8 elements;  NB. a slice
 28                     -- like A(23..45) raises CONSTRAINT_ERROR
 29 
 30    Mix (A(7..6));  Mix (A(234..4));   -- slices with null range
 31 
 32    -- Print all the values stored in the array "A"
 33    for I in A'Range loop
 34       Text_IO.Put ("A[");
 35       Integer_Text_IO.Put (Item=>I, Width=>2);
 36       Text_IO.Put ("] = ");
 37       Integer_Text_IO.Put (Item=>A(I), Width=>2);
 38       Text_IO.New_Line;
 39    end loop;
 40 
 41 end Sub;