简体   繁体   中英

Find Maximum Value in Array in Ada

I am doing an Ada program with lots of different functions messing with arrays, i got all my sorting functions going, i am now stuck on retrieving the maximum value in an array using a loop invariant to design the loop for that function. any help?

How about simply looping over the whole array?

something like this:

function Get_Maximum (Of : My_Array_Type) return Element_Type is
   Maximum : Element_Type := Of (Of'First);
begin
   for I in Of'First + 1 .. Of'Last loop
      if Of (I) > Maximum then
         Maximum := Of (I);
      end if;
   end loop;
   return Maximum;
end Get;

will raise an exception if the array is empty, but this is left as an excercise for the reader, if those cases are needed.

对于未排序的数组,oenone是正确的,但正如您所说,您的排序函数正常工作,为什么不对数组进行排序,然后使用:

Maximum := Of(Of'Last);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM