gnatprove_13.2.1_28fc3583/share/examples/spark/spark_book/Chapter-05/exercise.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Exercise is
   type Percent       is delta 0.25 range 0.0 .. 100.0;
   type Percent_Array is array (Integer range <>) of Percent;

   function Num_Below (List  : in Percent_Array;
                       Value : in Percent) return Natural
   -- Returns the number of values in List that are less than Value
      with
         Pre => (for all J in List'First .. List'Last - 1 =>
                     List (J) <= List (J + 1))
   is
      Result : Natural;
      Index  : Integer;
   begin
      Result := 0;
      Index  := List'First;
      loop
         exit when Index > List'Last or else List (Index) >= Value;
         Result := Result + 1;
         Index  := Index + 1;
      end loop;
      return Result;
   end Num_Below;

begin
   Put (Num_Below (List  => (11.2, 23.4, 33.4, 48.6, 51.6, 61.7, 71.5),
                   Value => 50.0));
end Exercise;