File : dotproduct.sp


$ spar dotproduct
 3


#!/usr/local/bin/spar

pragma annotate( summary, "dotproduct" )
       @( description, "Create a function/use an in-built function, to compute" )
       @( description, "the dot product, also known as the scalar product of two" )
       @( description, "vectors. If possible, make the vectors of arbitrary length." )
       @( description, "As an example, compute the dot product of the vectors [1," )
       @( description, " 3, -5] and [4, -2, -1]." )
       @( description, "If implementing the dot product of two vectors directly," )
       @( description, "each vector must be the same length; multiply" )
       @( description, "corresponding terms from each vector then sum the results" )
       @( description, "to produce the answer. " )
       @( see_also, "http://rosettacode.org/wiki/Dot_product" )
       @( author, "Ken O. Burtch" );
pragma license( unrestricted );

pragma restriction( no_external_commands );

procedure dotproduct is
  type vect3 is array(1..3) of integer;
  v1 : constant vect3 := (1,3,-5);
  v2 : constant vect3 := (4,-2,-1);

  sum : integer := 0;
begin
  if arrays.length( v1 ) /= arrays.length( v2 ) then
     put_line( standard_error, "different lengths" );
     command_list.set_exit_status( 193 );
     return;
  end if;
  if arrays.first( v1 ) /= arrays.first( v2 ) then
     put_line( standard_error, "different starts" );
     command_list.set_exit_status( 194 );
     return;
  end if;
  for p in arrays.first( v1 )..arrays.last( v1 ) loop
      sum := @ + v1(p)*v2(p);
  end loop;
  ? sum;
end dotproduct;

-- VIM editor formatting instructions
-- vim: ft=spar