Concepts / Finding the Maximum Value

Finding the Maximum Value

Data scanning loops follow a three-step construction: initialize variables before the loop, perform computation on each item inside the loop body, and examine results after the loop completes.

  • Programming

Why Scan Every Item

Many programming tasks require examining every item in a collection, such as a list, a file, or a sequence of user inputs. The task may be to locate the largest number, find the smallest value, or count items that meet a condition. A loop avoids writing separate instructions for each item and provides a repeatable way to examine the data.

A data scanning loop has three stages: initialize tracking variables before the loop, compute with each item inside the loop, and examine the result after the loop.

The Three-Stage Pattern

before looploop completesInitializeSet a starting resultScan each itemCompute during iterationExamine resultRead after the loop
What happens before the loop, during each iteration, and after the loop completes?

For maximum-finding, the tracking variable represents the largest value found so far. Before scanning begins, it receives a starting value. During each iteration, the current item is compared with that tracked value. If the current item is larger, the tracking variable is updated. If it is not larger, the tracking variable stays as it is. After every item has been processed, the final tracking value is examined as the result.

Tracing the Current Maximum

Consider the source trace in which max_value starts at 3 and the scan encounters values 7, 2, 9, and 5. The important question at each step is not simply which item is being visited. It is whether that item should replace the current maximum.

examine 7examine 2examine 9examine 53initial max_value7new maximum7after examining 29new maximum9after examining 5
How does the current maximum change, or stay the same, as each data item is examined?
Item examinedCurrent maximum after the comparisonState change
Starting value3Initialized
77Changed because 7 is larger than 3
27Stayed the same because 2 is not larger than 7
99Changed because 9 is larger than 7
59Stayed the same because 5 is not larger than 9

The tracking variable changes only when the incoming item is larger than the current maximum.

This trace shows selective updating. The loop examines every item, but max_value changes only twice: first from 3 to 7, and then from 7 to 9. The smaller values do not cause an update. Once the loop completes, the final value, 9, is examined as the result.

The Update Decision

incoming item is largercompareincoming item is not largercomparemax_valuecurrent maximumcurrent itemvalue being examinedNew maximumreplace tracked valueCurrent maximumkeep tracked value
How does each incoming value connect to the current maximum, and what determines whether the maximum is replaced?

The update decision is the central mechanism of the pattern. The current item and the tracked maximum are compared inside the loop. A larger current item causes a state transition: the tracked maximum becomes that item. A current item that is not larger causes no state transition: the tracked maximum remains unchanged. Therefore, the loop both scans continuously and updates selectively.

Do not expect the tracking variable to change on every iteration. Visiting an item and changing the state are different events.

Initialization and Final Examination

updates during scanning3starting max_value9final max_value
What value does the maximum variable start with, and how does that starting choice relate to the final result?

The maximum-tracking variable has two especially important moments. Before the loop, it is initialized so a comparison can take place. After the loop, it is examined because all items have then been processed. The source trace begins with max_value at 3 and ends with max_value at 9 after the larger values have replaced the earlier state.

  • Initializing the tracking variable inside the loop

    The variable needs a starting value before the loop and must retain updates from earlier iterations.

    Fix: Initialize the result-tracking variable before scanning begins.

  • Updating the maximum for every item

    State should change only when the comparison condition is met.

    Fix: Replace the current maximum only when the current item is larger.

  • Examining the result before the loop finishes

    The final result is available only after every item has been processed.

    Fix: Examine the tracking variable after the loop completes.

Adapting the Pattern

Finding a maximum is one use of a broader scanning pattern. The three stages remain in place when the goal changes: initialize before the loop, compute during each iteration, and examine after the loop. What changes is the tracking variable's starting value, the computation performed for each item, and the result examined at the end. This same structure supports finding a minimum, summing values, counting items, or searching for a specific value.

Scanning taskWhat changesWhat remains constant
Find a maximumTrack and conditionally replace the largest valueInitialize, process each item, examine after the loop
Find a minimumTrack and conditionally replace the smallest valueInitialize, process each item, examine after the loop
Sum valuesUpdate a running total for each itemInitialize, process each item, examine after the loop
Count itemsUpdate a count when the relevant condition is metInitialize, process each item, examine after the loop

Different scanning tasks vary in their initialization and update work, but share the same three-stage construction.

MEDIUM

Plan a scanning loop for finding the minimum value. Identify what must be initialized before the loop, what comparison or computation occurs for each item, and when the final result is examined.

Hints
  • Start by naming the result-tracking variable.
  • Decide what condition would justify replacing its current value.
  • Place the final examination after every item has been processed.

Reliable Scanning

  1. Initialize result-tracking variables before the loop so they have a starting value.
  2. Process each item inside the loop and use a comparison or test to decide whether the state changes.
  3. The current maximum changes only when the incoming item is larger; otherwise it remains unchanged.
  4. Examine the final tracking value after the loop has completed.
  5. The same initialize, compute, and examine pattern applies to maximums, minimums, sums, counts, and other scanning tasks.

Key Takeaways

  • A data scanning loop initializes tracking variables before the loop, computes with each item during the loop, and examines the result afterward.
  • Finding a maximum depends on comparing each incoming item with the current maximum.
  • The tracking state changes only when the comparison condition is satisfied.
  • A loop can examine every item while changing the tracked result only when necessary.
  • The same structure can be adapted to finding minimums, summing, counting, and other scanning tasks.