What is the difference between a counter and an accumulator? [closed]
Asked Answered
J

3

6

I'm not sure what the difference are, but here are the problems.

Write an assignment statement that updates a counter variable named numStudents by 1. would it be:

numStudents = numStudents + 1

? My other problem is

Write an assignment statement that updates an accumulator variable named total by the value in the sales variable. would it be identical like:

total = total + sales

?

Judicial answered 19/10, 2012 at 22:2 Comment(4)
What should we do with your questions? Shouldn't you be able to answer them from your studies so far? (Isn't that the point of homework?) BTW, it seems that you do understand the questions and know the answers.Patriciate
@KevinDTimm: unless there's a third question on the assignment not shown here, the assignment is not to explain the difference between a counter and an accumulator. It would be possible to complete the assignment if it said "update a banana variable named numStudents by 1", and "updates an aardvark variable named total by the value", you don't need to understand all the words to get the gist. That's the wondrous redundancy of natural language.Ulcerous
A counter implies an integer that goes 0,1,2,3,4 etc. (Up or down). An accumulator can do more such as collect sums that include floating point values.Lientery
I find the question quite interesting: while it's true that the actual programming problem is solved (even if it's just about assignments), there's a certain degree of depth in the question. And being worried about naming and purpose of variables is really important for a novice.Violetvioleta
V
7

There isn't a single, clear, univocal answer to your question.

It may be said that a counter is a variable that is incremented every time a given event is verified (e.g. when iterating an array, every time you encounter the number 5, you could increment a counter by 1).

The notion may be generalized because there are counters that are incremented by 2, or 3, or any value you'd like, on every step; however, the semantics of a counter usually lose sense if the step isn't always the same. It depends on the logic of your program, in the end: if you're keeping track of single and double rooms in an hotel, your numGuests counter could be incremented with steps of 1 and 2 depending on the rooms you're processing at a given moment, however it could be argued that in the end this is just for clarity or brevity, because the result is the same as incrementing it by 1, twice!

An accumulator is, instead, a variable that, for example, stores the sum of the elements of the array (i.e. you have not a fixed step, but the increment varies based on the elements you encounter).

The notion may be generalized for a list of items, applying a given function f repeatedly to the actual value of the accumulator and the next item of the list, saving the result in our accumulator. In this way, we have obtained the semantics of the fold high-order function (one of its other names is in fact accumulate). If we limit our analysis to what is called left fold, the accumulator stores at every moment an intermediate result, valid for the subset you already processed.

If you have an accumulator named total with the starting value of 0 and an array which contains the sales for the 12 months of the year, applying this definition with + (addition) as our f, will get you at the n-th step the sales for the first n months of the year.

Your examples look good then, as they're strictly adherent to these definitions of accumulators and counters.

Violetvioleta answered 19/10, 2012 at 23:5 Comment(0)
J
5

"counter" and "accumulator" mean different things to different people. What they mean to your professor only your professor can say.

That said, very often when someone refers to a counter what they mean is "count how many things there are [which meet these criteria]".

On the other hand, often when people say you should accumulate something, they mean "add up the total value of all these things [which meet these criteria]"

Here's an example. Suppose you have an array of students, and those students have the ages:

{16, 17, 16, 19}

Count how many students are 17 or older? The answer is 2. Accumulate the total age of the students age 17 or older? The answer is 19+16=35.

Jared answered 19/10, 2012 at 22:43 Comment(0)
U
2

In my experience, programmers use "counter" to mean something that counts things one at a time, or occasionally a fixed interval at a time. The same doesn't necessarily apply to an "accumulator", although that's not to say that something that counts things one at a time isn't an accumulator.

Ulcerous answered 19/10, 2012 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.