Find the maximum value in a list
Scenario. A procedure must find the largest numeric value in a list by inspecting each element.
Inputs. A non-empty finite list L of numbers. List positions start at 0. An empty list is invalid for this procedure.
Rules and notation. Output a value from the list that is greater than or equal to every element. length(L) is the number of elements.
Assumptions. Numeric comparisons are defined for every pair of elements. The list is not changed.
Listing 1 makes the structure for find the maximum value in a list explicit so you can identify its required fields and boundaries.
Deliverable. Trace largest and i for L = [-4, -2, -9]. Explain why initializing largest to 0 would be defective. Give expected outputs for [-4, -2, -9], [6], [3, 3], and [].
Success criteria. The trace outputs the largest element of the list, the initialization works for all-negative lists, a one-element list returns its element without entering the loop, and empty input follows the stated invalid policy.
Initialize from a real element so that largest is always the largest value among the elements inspected so far.