Find Maximum Value in Array in Ada

别来无恙 提交于 2019-12-11 07:58:53

问题


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?


回答1:


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.




回答2:


oenone is correct for an unsorted array, but as you state you have your sorting functions working correctly, why not sort the array, and then use:

Maximum := Of(Of'Last);


来源:https://stackoverflow.com/questions/7789203/find-maximum-value-in-array-in-ada

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!