# Difference between revisions of "Using an Accumulator"

(→Using two accumulators to find the mean.) |
(→Using two accumulators to find the mean.) |
||

Line 127: | Line 127: | ||

<question className="P" title="Using two accumulators to find the mean."> | <question className="P" title="Using two accumulators to find the mean."> | ||

<shell lang="java" className="P" import="shells.xml#main"/> | <shell lang="java" className="P" import="shells.xml#main"/> | ||

+ | <p class='imper'>Print the mean of all items in t he list.</p> | ||

*We initialise both <code>sum</code> and <code>count</code> to 0. | *We initialise both <code>sum</code> and <code>count</code> to 0. | ||

*We add to the sum and increment the count. | *We add to the sum and increment the count. |

## Revision as of 00:22, 8 December 2007

When using an accumulating variable there are three stages:

- Initialise
- We declare our accumulator and set it to an initial values.
- Update
- Inside the loop we update the accumulator - we take into account the current data item.
- Output/use
- After the loop our accumulator contains the required value. We use it or output it.

## Contents

- 1 Using an accumulator to add.
- 2 Using an accumulator to count.
- 3 Using an accumulator to count on a condition.
- 4 Using an accumulator to multiply.
- 5 Using an accumulator to find the maximum.
- 6 Using an accumulator to concatenate.
- 7 Using two accumulators to find the mean.
- 8 Using an accumulator to calculate.

## Using an accumulator to add.

Print the total of all of the numbers

- We initialise to 0.
- We add to the accumulator.
- The value printed is the sum 0+2+7+1+1.

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to count.

Print the number of items in the list.

- We initialise to 0.
- We increment the accumulator.
- The value printed is the count: 0+1+1+1+1.

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to count on a condition.

Count the number of 1s in the list

- We initialise to 0.
- We increment the accumulator only if the current value equals 1.

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to multiply.

Print the product of the numbers.

- We initialise to 1. (If you initialise to 0 then the result will always be 0.)
- We multiply the accumulator.
- The value printed is the product: 1*2*7*1*1

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to find the maximum.

Print the largest number in the list.

- We initialise to 0.
- We take the max.
- The value printed is the largest:

At each stage the accumulator stays the same or gets bigger (set to the current value).

max(max(max(max(0,2),7),1),1)

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to concatenate.

Concatenate all items in the list

Notice that we are using a list of strings this time.

- We initialise to the empty string "".
- We concatenate the next value (stick at the end).

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using two accumulators to find the mean.

Print the mean of all items in t he list.

- We initialise both
`sum`

and`count`

to 0. - We add to the sum and increment the count.
- We divide the sum by the count to get the average.

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]

## Using an accumulator to calculate.

- We initialise to 0.
- We multiply by the radix and add the next digit.
- By the end the number 2 has been multiplied by 10 three times, the number has been multiplied by 10 twice...
- 10*(10*(10*2+7)+1)+1

[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]