Task

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.




 
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.
 