Small Steps III — Thinking with Programs

Find the Largest Value

Find the Largest Value

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Remembering the largest value so far

What you will learn

Finding a maximum means remembering the largest value seen so far and replacing it when a larger one appears.

Try it

Example · example1.cpp
void small_main()
{
    Array<int> numbers = {7, 2, 9, 4, 5};
    int largest = numbers[0];

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] > largest)
            largest = numbers[i];

    print("Largest: ", largest);
}

Start with the first element, 7. 2 is smaller, so nothing changes; 9 is larger, so it replaces the remembered value. The result is Largest: 9.

Starting at 0 may be wrong when all values are negative. This example uses an array with at least one element.

Remembering a value's position

What you will learn

Remembering an index instead of a value lets you know both what the smallest value is and where it is.

Try it

Example · min_max_2_example2.cpp
void small_main()
{
    Array<int> numbers = {7, 2, 9, 1, 5};
    int smallest_index = 0;

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] < numbers[smallest_index])
            smallest_index = i;

    print("Smallest: ", numbers[smallest_index]);
    print("Index: ", smallest_index);
}

smallest_index is the position of the smallest element. Read its value with numbers[smallest_index]. In this example, the smallest value is 1 at position 3.

Index in the variable name helps distinguish a position from a value. If the minimum occurs several times, the current < comparison keeps its first occurrence.

Exercise

Find and print the smallest value in the Array.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Array<int> numbers = {8, -3, 5, 2, -1};

    // find and print the smallest value.
}

Hint

Use the first value as smallest and compare from index 1 onward.

show solution
void small_main()
{
    Array<int> numbers = {8, -3, 5, 2, -1};
    int smallest = numbers[0];

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] < smallest)
            smallest = numbers[i];

    print(smallest);
}
Try More (Optional)

Exercise

Find the index of the largest value, and print both the value and its index.

Exercise starter · min_max_2_exercise2_starter.cpp
void small_main()
{
    Array<int> numbers = {4, 11, 6, 20, 9};

    // find the index of the largest value.
}

Hint

Start largest_index at 0 and compare numbers[i] > numbers[largest_index].

show solution
void small_main()
{
    Array<int> numbers = {4, 11, 6, 20, 9};
    int largest_index = 0;

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] > numbers[largest_index])
            largest_index = i;

    print("Value: ", numbers[largest_index]);
    print("Index: ", largest_index);
}
← Previous lessonNext lesson →