/
Accumulators in Programming Accumulators in Programming

Accumulators in Programming - PowerPoint Presentation

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
344 views
Uploaded On 2019-11-23

Accumulators in Programming - PPT Presentation

Accumulators in Programming Assumes you have knowledge of the assignment operator and a loop What is an accumulator An accumulator is not new syntax it is just a new way to using the assignment operator ID: 767256

number accumulator sum numbers accumulator number numbers sum display calculator counter variable dozencount form punch hit shows entered total

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Accumulators in Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Accumulators in Programming Assumes you have knowledge of the assignment operator = and a loop

What is an accumulator? An accumulator is not new syntax, it is just a new way to using the assignment operator It is a logical concept that is used in most languages The general idea is that a variable is given the job of holding the “total” of several values. “Total” is in quotes because it is not always an arithmetic sum, it can be done using many different operations. An accumulator could hold the sum of 10 numbers, the concatenation (“sticking together”) of 5 strings, the product of 15 numbers, etc. An accumulator starts at a known value (like zero), and is changed by “adding” on a different value. The result is stored right back in the same variable.

Calculator Analogy Suppose you wanted to add up a bunch of numbers using a calculator You would first clear the display of the calculator so that it showed a zero. (You usually don’t want previous results mixed in with your total.) You punch in a number and hit the + key The display shows the first number (since it’s the sum of zero and the number) You punch in another number and hit + The display shows the sum of the two numbers entered. You repeat the process until you have entered as many numbers as you needed the sum of. The number in the display is your accumulator.

A Counter – a special form of an accumulator Sometimes you need to know how many times something happens The number of aliens killed The number of numbers read in An accumulator is useful here too You can add a constant term to an accumulator variable If you do, that is called a counter A counter is a specialized form of an accumulator Just changes the accumulator by a constant value Num_count = Num_count + 1 DozenCount = DozenCount + 12