Algorithm : Totals

If a sequence of numeric items is to be input and added up then a variable is used for the total and this will need to be initialised to 0.

Example

10 marks are to be input and added together.

A variable called total will be used to store the added marks. Each time a mark is input, it is added to the total.

The pseudocode algorithm will be...

total := 0
for 10 times do
  input ( mark)
  total := total + mark
end do
output(total)

 

 

Another example (combining rogue value, count and total)

Marks are to be input terminated by a rogue value of -99. The average of the marks is to be calculated.

The pseudocode algorithm for this problem would be..

count := 0
total := 0
repeat
  input (mark)  
  if mark is not rogue value then do  
    increment count  
    add mark to total  
  end do  
until mark = rogue value
average := total / mark
output (average)

Note : Pseudocode is not rigorous - I have put
'
add mark to total'

instead of
'total := total + mark'

This is not important as long as
(a) the
meaning of the instruction is clear, and
(b) the code shows the
structure...and indenting the 'blocks' is really really really important.