2.2.18 Aggregating Values With a Loop

Calculate Average Goals Across Training Rounds

The coach wants average goals per planned round, including rounds in which no goal was scored. A loop can first combine the scores into a total; a calculation after the loop can turn that total into an average.

Work Through the Soccer Example

Place both methods below inside the existing Game class. The sample helper returns scores 2, 0, and 3 for rounds 1 through 3, and zero for other numbers.

Assume plannedRounds is nonnegative. For a plan of zero, this application reports 0.0 instead of attempting division.

Calculate Average Goals Across Training Rounds
public int GetRoundGoals(int round)
{
    if (round == 1)
    {
        return 2;
    }

    if (round == 3)
    {
        return 3;
    }

    return 0;
}

public double GetAverageTrainingGoals(int plannedRounds)
{
    int totalGoals = 0;

    for (int round = 1; round <= plannedRounds; round++)
    {
        int goals = this.GetRoundGoals(round);
        totalGoals = totalGoals + goals;
    }

    if (plannedRounds == 0)
    {
        return 0.0;
    }

    double average = (double)totalGoals / plannedRounds;
    return average;
}
Follow the Values and Execution Order

Build the total before calculating the average

Aggregation combines multiple values into a summary. Here, the body reads one score and adds it to totalGoals.

For a plan of 3, the running totals become 2, 2, and 5. The average is calculated only after all three scores have been included.

Include zero-goal rounds in the denominator

The question is goals per planned round. Round 2 contributes zero goals, but it is still one of the three rounds.

Divide the total of 5 by 3. Do not divide by only the rounds with positive scores, because that would answer a different question.

Use the numeric conversion you learned earlier

Both inputs to the division are stored as ints. Writing (double)totalGoals converts the numerator before division.

The calculation can therefore produce approximately 1.67 rather than discarding the fraction through integer division. The method returns the unrounded double result; it does not format a displayed number.

Use a small case to check the summary

A plan of 1 includes only the first score, 2. Its average is 2.0. A plan of 3 includes all three scores and returns 5.0 / 3.

These cases check both traversal and the final calculation.

Check the units of the summary

The numerator is goals across the included rounds. The denominator is the number of planned rounds. Dividing them produces goals per planned round, which is the coach's requested summary.

The zero-goal round contributes no goals but still contributes one round to that denominator. Leaving it out would change the question being answered, not simply improve the precision of the same answer.

Trace This Example
Totals and averages for three plans
Planned rounds Scores included Total goals Returned result
0 None 0 0.0 by reporting rule
1 2 2 2.0
3 2, 0, 3 5 Approximately 1.67
Read the Matching Activity Diagram
Calculate Average Goals Across Training Rounds.
Calculate Average Goals Across Training Rounds
  • New
  • Changed
  • Removed

The loop in the diagram builds the total. Only after its exit does execution reach the zero-plan decision.

The true route returns 0.0 without dividing. The false route calculates the double average and returns it. Both routes occur after the repeated additions, matching the method.

Try a Focused Change
Predict and Explain

Check your reasoning

The loop includes scores 2 and 0, giving a total of 2. There are two planned rounds, so the average is 2.0 / 2, or 1.0.

The zero-goal second round must still be included in that denominator.