How to resolve the algorithm Greatest element of a list step by step in the Oberon-2 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 Oberon-2 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 Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE GreatestElement1;
IMPORT
ADT:ArrayList,
Object:Boxed,
Out;
VAR
a: ArrayList.ArrayList(Boxed.LongInt);
max: Boxed.LongInt;
PROCEDURE Max(al: ArrayList.ArrayList(Boxed.LongInt)): Boxed.LongInt;
VAR
i: LONGINT;
item, max: Boxed.LongInt;
BEGIN
max := NEW(Boxed.LongInt,MIN(LONGINT));
i := 0;
WHILE (i < al.size) DO
item := al.Get(i);
IF item.value > max.value THEN max := item END;
INC(i)
END;
RETURN max
END Max;
BEGIN
a := NEW(ArrayList.ArrayList(Boxed.LongInt),5);
a.Append(NEW(Boxed.LongInt,10));
a.Append(NEW(Boxed.LongInt,32));
a.Append(NEW(Boxed.LongInt,4));
a.Append(NEW(Boxed.LongInt,43));
a.Append(NEW(Boxed.LongInt,9));
max := Max(a);
Out.String("Max: ");Out.LongInt(max.value,4);Out.Ln
END GreatestElement1.
MODULE GreatestElement2;
IMPORT
Out;
VAR
a: ARRAY 10 OF LONGINT;
PROCEDURE Max(a: ARRAY OF LONGINT): LONGINT;
VAR
i, max: LONGINT;
BEGIN
max := MIN(LONGINT);
FOR i := 0 TO LEN(a) - 1 DO
IF a[i] > max THEN max := a[i] END;
END;
RETURN max
END Max;
BEGIN
a[0] := 10;
a[1] := 32;
a[2] := 4;
a[3] := 43;
a[4] := 9;
Out.String("Max: ");Out.LongInt(Max(a),4);Out.Ln
END GreatestElement2.
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the 11l programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the APL programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Ol programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Fōrmulæ programming language