What is the difference between a meter and a battery?

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

Write an assignment statement that updates the counter variable named numStudents to 1. this will be:

numStudents = numStudents + 1

? My other problem:

Write an assignment statement that updates the battery variable named total by value in the sales variable. it would be the same:

total = total + sales

?

+4
source share
3 answers

There is no single, definite answer to your question.

We can say that the counter is a variable that increases every time this event is checked (for example, when repeating the array every time you encounter the number 5, you can increase the counter by 1).

The concept can be generalized, because at each step there are counters that increase by 2, 3 or any value; however, counter semantics usually lose their meaning if the step is not always the same. It depends on the logic of your program, in the end: if you track single and double rooms in a hotel, your numGuests counter may increase in increments of 1 and 2 depending on the numbers you are processing at the moment, however, it can be argued that in the end it’s easy for clarity or brevity, because the result is the same as incrementing it by 1 , twice!

The battery, instead, is a variable that, for example, stores the sum of the elements of the array (i.e. you do not have a fixed step, but the increment changes depending on the elements you encounter).

The designation can be generalized to a list of elements by repeatedly applying the given function f to the actual value of the accumulator and the next element of the list, storing the result in our accumulator. Thus, we got the semantics of the fold high-order function (in fact, one of its other names is accumulating). If we limit our analysis to what is called the left fold, the accumulator at each moment maintains an intermediate result that is valid for the already processed subset.

If you have a drive with the name total with a starting value of 0 and an array that contains sales for 12 months of the year, using this definition with + (addition) as our f , you will receive at the nth step sales for the first n months of the year.

Your examples look good, as they strictly adhere to these definitions of batteries and counters.

+4
source

In my experience, programmers use a "counter" to mean something that takes things into account one at a time, or sometimes a fixed interval at a time. The same does not necessarily apply to a β€œbattery”, although this does not mean that something that counts things one at a time is not a battery.

+2
source

β€œcounter” and β€œbattery” mean different things to different people. What they mean for your professor can only be said by your professor.

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

On the other hand, often when people say that you have to accumulate something, they mean "summarize the total value of all these things [that meet these criteria]"

Here is an example. Suppose you have many students and these students have an age:

 {16, 17, 16, 19} 

Indicate how many students are over 17 years old? Answer: 2. Accumulate the total age of students aged 17 years and older? The answer is 19 + 16 = 35.

+2
source

Source: https://habr.com/ru/post/1440886/


All Articles