Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 1.58 KB | None | 0 0
  1. with Ada.Integer_Text_IO;
  2.  
  3. package body Max2 with SPARK_Mode is
  4.    function FindMax2 (V : Vector) return Integer is
  5.       Const : Boolean := True;
  6.       Max1 : Integer := 0;
  7.       Max2 : Integer := 0;
  8.    begin
  9.       if V'Length = 2 then
  10.          if V(V'First) > V(V'Last) and then V(V'Last) >= 0 then return V(V'Last);
  11.             elsif V(V'First) < V(V'Last) and then V(V'First) >= 0 then return V(V'First);
  12.             else return 0;
  13.          end if;
  14.       elsif V'Length = 1 then
  15.          return 0;
  16.       else
  17.              
  18.          for I in Integer range V'Range loop
  19.             pragma Loop_Invariant (Max1 >= Max2);
  20.             pragma Loop_Invariant (Max2 >= 0);
  21.             pragma Loop_Invariant (if const = True then (for all Index1 in V'First .. I =>
  22.                                                            (for all Index2 in V'First .. Index1 =>
  23.                                                               (V(Index1) = V(Index2)))));
  24.        
  25.             if I > V'First then
  26.                if V(I) /= V(I - 1) then
  27.                   const := False;
  28.                end if;
  29.             end if;
  30.            
  31.             if I = V'Last and then const = True then
  32.                return 0;
  33.             end if;
  34.            
  35.            
  36.             if V(I) > Max1 then
  37.                Max2 := Max1;
  38.                Max1 := V(I);
  39.             end if;
  40.          
  41.             if (V(I) < Max1 and V(I) > Max2) then
  42.                Max2 := V(I);
  43.             end if;
  44.              
  45.          end loop;
  46.       end if;
  47.      
  48.      
  49.       return Max2;
  50.    end FindMax2;
  51. end Max2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement