The Ada Program: mult.adb

  1 -- mult.adb: program with matrix multiplication subprocedure
  2 
  3 procedure Mult is
  4 
  5    type Matrix is
  6        array (Positive range <>, Positive range <>) of Integer;
  7 
  8    -- Matrix multiplication
  9    procedure Multiply (A,B: in Matrix; C: out Matrix) is
 10    begin
 11 
 12       pragma Assert (A'First(2)=B'First(1) and A'Last(2)=B'Last(1));
 13       pragma Assert (C'First(1)=A'First(1) and C'Last(1)=A'Last(1));
 14       pragma Assert (C'First(2)=B'First(2) and C'Last(2)=B'Last(2));
 15 
 16       for Row in A'Range loop
 17          for Col in B'Range(2) loop
 18             declare
 19                Sum: Integer := 0;
 20             begin
 21                for I in A'Range(2) loop
 22                   Sum := Sum + A(Row,I)*B(I,Col);
 23                end loop;
 24                C(Row,Col) := Sum;
 25             end;
 26          end loop;
 27       end loop;
 28    end Multiply;
 29 
 30    N: constant := 10;
 31    A,B: Matrix (1..N, 1..N) := (1..N=>(1..N=>1));
 32    C: Matrix (1..N, 1..N);
 33 
 34 begin
 35 
 36    Multiply (A,B,C);
 37    Multiply (A,B,A);  -- does not work as expected.
 38 
 39 end Mult;