basics · intermediate · ~15 min
Accumulate persistent state safely.
Maintain a running sum across calls using a static local.
Implement long running_total(int x) that adds x to an internal running sum (which starts at 0) and returns the new total. The sum must persist between calls via a static local. No main — the grader calls it.
A single int x to add to the running total.
Returns the updated running total, as a long.
running_total(5) -> 5
running_total(10) -> 15
running_total(-3) -> 12
static local so it survives between calls.A single int x to add to the running total.
The updated running total, as a long.
Use a static local; the sum starts at 0.
long running_total(int x) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.