Skip to content

Exercise 9 of 10 · Accumulation

Total the Donations

What you will make

A program that adds up a list of donation amounts using a loop and prints the grand total.

The one new idea: Accumulating a running total in a loop

The accumulator pattern — starting a variable at zero and adding to it on every loop pass — is how programs compute sums, counts, and totals over any amount of data, and it's one of the most widely reused patterns in programming.

Go straight to the code ↓

The concept

When you need to add up every value in a collection, the standard approach is the accumulator pattern: create a variable, start it at 0, and on every pass through a loop, add the current value to it. Written out, that's total <- total + donations[i] inside the loop body — the key detail is that the accumulator variable appears on both sides of the assignment, so its previous value is preserved and added to, rather than replaced.

If you leave out total + and just write total <- donations[i], the loop still runs without any error, but each pass simply overwrites total with the current item, discarding everything accumulated before it. After the loop finishes, total would hold only the very last value processed.

Worked example

r
song_lengths <- c(3, 4, 5)
total <- 0

for (i in 1:length(song_lengths)) {
  total <- total + song_lengths[i]
}

cat("Total minutes: ", total, "\n", sep = "")
Output
Total minutes: 12

Here, total starts at 0. On the first pass it becomes 0 + 3 = 3, then 3 + 4 = 7, then 7 + 5 = 12. Each pass builds on the result of the one before it, which is exactly what makes the final total correct.

Your turn

The starter code is meant to add up four donation amounts, but the line inside the loop replaces total with just the current donation instead of adding to it. Fix that line so it keeps the running sum from previous passes and adds the current donation on top of it.

If something goes wrong

If the final total matches only the last number in the donations vector (5), that confirms the accumulator is being overwritten rather than added to — check that the line inside the loop includes total + before donations[i]. If the total looks too large, double-check that total starts at 0 before the loop begins, rather than at some other value.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Forgetting to include total on the right-hand side of the assignment.
Without total + donations[i], the line only stores the current donation, discarding the running sum built up from every earlier pass through the loop.
Starting total at a value other than 0, like the first donation amount.
Starting at 0 is what makes the accumulator pattern correct for any list, including one that might have a first value of 0 itself; starting elsewhere would either skip or double-count a value.
Assuming the bug causes an error.
This code runs without any error message at all — it just silently ends up with the wrong number (only the last donation) instead of the true sum, so you have to check the printed total, not just whether the program crashes.

Want a blank editor instead? Open the R playground.