The Ada Program: store.adb

  1 -- store.adb:  sales of a store in a 4 dimensional array
  2 
  3 with Ada.Text_IO;
  4 use Ada;
  5 
  6 procedure Store is
  7 
  8    type Month_Type is (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  9    type Dept_Type is (Purchasing, Accounting, Executive, Shipping);
 10 
 11    subtype Store_Range is Positive range 1..10;
 12    subtype Item_Range is Positive range 1..100;
 13 
 14    -- 4D unconstrainted array;  all must be unconstrainted if any are
 15    type Sales_Array is array (
 16       Store_Range range <>,
 17       Month_Type range <>,
 18       Item_Range range <>,
 19       Dept_Type range <>) of Integer;
 20 
 21    type Monthly_Sales_Array is array (Month_Type range <>) of Integer;
 22 
 23    procedure Monthly_Sales (X:Sales_Array; Y:out Monthly_Sales_Array) is
 24       Sum: Integer;
 25    begin
 26 
 27       -- Y'Range must be a subset of X'Range(2)
 28       pragma Assert (X'First(2) < Y'First);
 29       pragma Assert (Y'Last     < X'Last(2));
 30 
 31       for Month in Y'Range loop
 32          Sum := 0;
 33          for Store in X'Range(1) loop
 34             for Item in X'Range(3) loop
 35                for Dept in X'Range(4) loop
 36                   Sum := Sum + X (Store, Month, Item, Dept);
 37                end loop;
 38             end loop;
 39          end loop;
 40          Y (Month) := Sum;
 41       end loop;
 42    end Monthly_Sales;
 43 
 44 begin
 45    null;
 46 end Store;