[英]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. 我正在做一个Ada程序,有很多不同的函数搞乱数组,我得到了所有的排序函数,我现在停留在使用循环不变量检索数组中的最大值来设计该函数的循环。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.