Counting, Sums, Averages, and Extremes

Three more standard algorithms read a sequence of values one at a time. They count the values that meet a condition, compute a sum or an average, and find the smallest or largest value. Here the values are a week of high temperatures. The static method Weather.high(day) returns the high, in degrees, for day 1 through day 7.
day    1   2   3   4   5   6   7
high 12 15 9 14 18 11 13
How often a condition holds
int warmDays = 0;
for (int day = 1; day <= 7; day++)
{
if (Weather.high(day) > 13)
{
warmDays++;
}
}
System.out.println(warmDays);
The loop runs seven times, but the counter goes up only on days 2, 4, and 5, so this prints 3. The counter is declared and set to 0 before the loop. Declared inside the body, it would start over at 0 on every pass and could never reach more than 1, and the print after the loop wouldn't compile, since the counter wouldn't exist there.
A sum and an average
A sum starts at 0 and adds one value per pass.
int total = 0;
for (int day = 1; day <= 7; day++)
{
total = total + Weather.high(day);
}
The total is 92. The average is the total divided by 7, and total / 7 is 13. Since total and 7 are both int values, integer division drops the remainder. A cast on one operand makes the division a double division: (double) total / 7 is about 13.14. The cast has to reach an operand before the division happens. Written as (double) (total / 7), the division happens first, and the result is 13.0.
The largest value
A tracker holds the largest value seen so far, and each new value replaces it only if the new value is larger.
int highest = Weather.high(1);
for (int day = 2; day <= 7; day++)
{
if (Weather.high(day) > highest)
{
highest = Weather.high(day);
}
}
The tracker starts at 12, becomes 15 on day 2, and becomes 18 on day 5, and no later day beats 18, so highest ends at 18. The loop starts at day 2 because day 1 already set the starting value.
Starting the tracker at a convenient number instead is the usual mistake. A tracker for the lowest high, started at 0 and replaced only by a smaller value, never changes this week, since every high is above 0. It reports 0, a temperature that isn't in the data. Starting from the first value works whatever the values turn out to be. The lowest high is 9, on day 3.