How to resolve the algorithm Greatest element of a list step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Greatest element of a list step by step in the Ada programming language
Table of Contents
Problem Statement
Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Greatest element of a list step by step in the Ada programming language
Source code in the ada programming language
with Ada.Text_Io;
procedure Max_Test isco
-- substitute any array type with a scalar element
type Flt_Array is array (Natural range <>) of Float;
-- Create an exception for the case of an empty array
Empty_Array : Exception;
function Max(Item : Flt_Array) return Float is
Max_Element : Float := Float'First;
begin
if Item'Length = 0 then
raise Empty_Array;
end if;
for I in Item'range loop
if Item(I) > Max_Element then
Max_Element := Item(I);
end if;
end loop;
return Max_Element;
end Max;
Buf : Flt_Array := (-275.0, -111.19, 0.0, -1234568.0, 3.14159, -3.14159);
begin
Ada.Text_IO.Put_Line(Float'Image(Max(Buf)));
end Max_Test;
generic
type Item is digits <>;
type Items_Array is array (Positive range <>) of Item;
function Generic_Max (List : Items_Array) return Item;
function Generic_Max (List : Items_Array) return Item is
Result : Item := List (List'First);
begin
for Index in List'First + 1..List'Last loop
Result := Item'Max (Result, List (Index));
end loop;
return Result;
end Generic_Max;
You may also check:How to resolve the algorithm Algebraic data types step by step in the F# programming language
You may also check:How to resolve the algorithm DNS query step by step in the PHP programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the REXX programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the D programming language
You may also check:How to resolve the algorithm 21 game step by step in the REXX programming language