← Skill tree CS Skill Tree 0 CSCD210

Arrays and Accumulator Patterns

Textbook: BJP (Reges and Stepp)

Where you are: Week 0 review > Arrays and accumulator patterns

Try This First

For the array {5, 2, 8, 1, 9, 3}, add up all six values in your head before reading on.

Reveal

28. A sum accumulator starts at 0 and adds each element as a loop walks the array: 5, 7, 15, 16, 25, 28.

Before You Start

Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.

See Loops: for, while, and do-while for the full loop lesson.

Not sure? Take the 60-second self-check.
  1. What does a loop let you avoid writing by hand? The same statement repeated many times; the loop runs the body once per pass.
  2. What controls how many times a for loop runs? Its condition, checked before each pass, together with the update that moves toward stopping.

What You Need To Walk In With

Walk into the next class able to state these:

You should be able to: write a sum, count, min, or max loop over an int[], and index an array correctly.

How It Works

Declaring and walking an array

int[] xs = {5, 2, 8, 1, 9, 3};
System.out.println(xs.length);   // 6
System.out.println(xs[0]);       // 5  (first element)
System.out.println(xs[5]);       // 3  (last element, length - 1)

An index outside 0 to length - 1 throws an error at runtime. Walk the array with an indexed loop:

First time seeing an indexed for loop? Open for a 20-second refresher.

A for loop has three parts: a start value (int i = 0), a condition checked before each pass (i < xs.length), and an update after each pass (i++). On each pass, i holds the current index. See Loops: for, while, and do-while for the full picture.

for (int i = 0; i < xs.length; i++) {
    System.out.println(xs[i]);
}

The accumulator family

Each pattern declares a variable before the loop and updates it once per element:

int sum = 0;
int max = xs[0];               // seed from the first element
for (int i = 0; i < xs.length; i++) {
    sum = sum + xs[i];
    if (xs[i] > max) { max = xs[i]; }
}

A count adds 1 when a test passes; a min mirrors the max with <. The accumulator must be declared before the loop so it survives across iterations and is in scope after.

Worked Example: Predict, Then Check

int[] xs = {4, 9, 2};
int max = xs[0];
for (int i = 1; i < xs.length; i++) {
    if (xs[i] > max) { max = xs[i]; }
}
System.out.println(max);

Predict the output before reading on.

Reveal

9. max starts at 4, stays 4 when compared with... it becomes 9 at index 1, then 9 is not beaten by 2, so the result is 9.

A Common Mistake

Seeding a min or max with 0 is a frequent bug. For the array {3, 7, 2, 5}, a min seeded at 0 never finds an element smaller than 0, so it returns 0, a value not even in the array. Seed from xs[0] and start the loop at index 1. (Source: BJP (Reges and Stepp), Ch 7.)

Go Deeper (optional)

For the curious: every accumulator is the same shape, a fold of the array into one value: start with a seed, combine it with each element, and read the result. Sum seeds with 0 and combines with plus; max seeds with the first element and combines with “keep the larger.” Seeing the shared shape makes a new accumulator (a product, a concatenation) easy to write.

Check Yourself

Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.

Check your understanding

Sum of the array {5, 2, 8, 1, 9, 3}?

Tier 1 · BJP (Reges and Stepp), Ch 7

Where do you declare a sum accumulator?

Tier 1 · BJP (Reges and Stepp), Ch 7

To find the minimum of {3, 7, 2, 5}, what should you seed the running min with?

Tier 2 · BJP (Reges and Stepp), Ch 7

For int[] xs = {5, 2, 8}; what is xs.length and the last valid index?

Tier 1 · BJP (Reges and Stepp), Ch 7