Codementor Events

Simple Algorithmic Pattern Series — The Accumulator Pattern

Published May 10, 2019Last updated Aug 05, 2019

Imperative programming languages, such as C, C#, Java, C++ and many others allow modifying the value of a variable.  We use this mutablility in the accumulator pattern, to start a variable, the accumulator, at one value, and modify it as needed, so that the answer we're looking for appears after the accumulator pattern is done.

When we need to identify the state or value of something by conditional logic or repetative search, we can use the accumulator pattern.

The accumulator pattern has two basic parts:

  • Declare a variable that serves as the accumulator — and initialize the accumulator with a meaningful default value,

  • Test some conditions — and change the accumulator based on the desired condition.

During execution of these two parts, the accumulator is in the process of getting the right answer — it is not necessarily correct right of the bat, but it will change so that when these parts are completed, the accumulator variable will hold the answer we're looking for.

Here are some examples:

A simple example is to determine whether one of a set of conditions is true:

bool answer = false;
if ( condition ) answer = true;
else if ( otherCondition ) answer = true;

During the execution of the parts, the accumulator variable (here answer) starts as false, is updated during the conditional portion, and in the end holds the answer of interest.

Ok, that was a simple example, here are some more-recognizable examples:

SUM

To compute the SUM of a series like an array we initialize an accumulator to 0, which is the additive identity, and then, for each element in the series, we add to the accumulator (here given array A and it's length N):

int sum = 0;
for ( int i = 0; i < N; i++ )
    sum += A [ i ];

MIN

To compute the MIN of a series like an array we initialize an accumulator to MAX_INT, which is the largest possible integer, and then, for each element in the series, we see capture it if it is smaller than what we have now:

int min = MAX_INT;
for ( int i = 0; i < N; i++ )
    if ( A [ i ] < min )
        min = A [ i ];

MAX

To compute the MAX of a series like an array we initialize an accumulator to MIN_INT, which is the largest possible integer, and then, for each element in the series, we see capture it if it is larger than what we have now:

int max = MIN_INT;
for ( int i = 0; i < N; i++ )
    if ( A [ i ] > max )
        max = A [ i ];

AVG

To compute the average (AVG) we do SUM and divide by the count:

int sum = 0;
for ( int i = 0; i < N; i++ )
    sum += A [ i ];
double avg = sum / N;

The accumulator pattern can be applied to many computations.  The important thing to realize is that the accumulator isn't "correct" until both parts are finished.

Discover and read more posts from Erik Eidt
get started
post commentsBe the first to share your opinion
Nicki Nixon
5 years ago

Thanks for this!
There are a couple errors though. The MIN and MAX signs should be flipped.

MIN:
int min = MAX_INT;
for ( int i = 0; i < N; i++ )
if ( min > A [ i ] ) //not less than A
min = A [ i ];

and

MAX:
…if ( min < A [ i ] )

Erik Eidt
5 years ago

Thank you! I’ll fix this when I’m back in the office.

Show more replies