Small Steps III — Thinking with Programs

Calculate Totals and Counts

Calculate Totals and Counts

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.

Adding values in order

What you will learn

An algorithm is a procedure for finding an answer. Calculate a sum by remembering the total so far and adding the next value.

Try it

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

    for (int i = 0; i < numbers.length(); i = i + 1)
        total = total + numbers[i];

    print("Sum: ", total);
}

total changes from 0 → 3 → 10 → 12 → 21 → 25. The expected output is Sum: 25.

Before any values have been added, their sum is 0, so the initial value is 0. Creating total with 0 inside every iteration prevents accumulation.

Counting matching values

What you will learn

To count matches, add 1 only when you encounter an element that meets the condition.

Try it

Example · sum_count_2_example2.cpp
int count_even(Array<int> numbers)
{
    int count = 0;

    for (int i = 0; i < numbers.length(); i = i + 1)
        if (numbers[i] % 2 == 0)
            count = count + 1;

    return count;
}

void small_main()
{
    Array<int> numbers = {3, 8, 4, 7, 10};
    print("Even: ", count_even(numbers));
}

numbers[i] % 2 == 0 asks whether division by 2 has remainder 0: whether the number is even. 8, 4, and 10 match, producing Even: 3. Zero also meets this condition.

count_even takes an array parameter. Here, it receives a copy of an Array<int> value; later lessons on references show how to avoid copying.

Exercise

Find an Array's sum, divide by its number of values, and print the average. Make total a double so the division can produce a fractional result.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Array<int> numbers = {10, 20, 30, 40};
    double total = 0;

    // Add the values and print the average.
}

Hint

Start with double total = 0; and calculate total / numbers.length() at the end.

show solution
void small_main()
{
    Array<int> numbers = {10, 20, 30, 40};
    double total = 0;

    for (int i = 0; i < numbers.length(); i = i + 1)
        total = total + numbers[i];

    print("Average: ", total / numbers.length());
}
Try More (Optional)

Exercise

Count and print how many values in the Array are greater than 0.

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

    // Count positive values.

    print(count);
}

Hint

Check numbers[i] > 0 for each value and increase count by 1 when true.

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

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

    print(count);
}
← Previous lessonNext lesson →