Running Totals

Running Totals are used to add a list of numbers.

General Algorithm

set total to zero
for each value in list
    add value to the total
next value

Adding numbers as they are entered (Nat4)

01
02
03
04
05
06
07
08
09
10
11
12
13
#
totalAge = 0
 
for pupil in range(5):
 
    age = int(input('Enter age:'))
    totalAge = totalAge + age
 
print(f'Total age = {totalAge}')
 
averageAge = totalAge / 5
print(f'Average = {averageAge}')
#

Adding numbers in an array (Nat5)

01
02
03
04
05
06
07
08
09
10
#
scores = [4,7,9,4,8,2,7]
 
total = 0
 
for player in range(len(scores)): # or range(7):
 
    total += scores[player]
  
#

Adding numbers in an array of records (Higher)

Traversing array by index:

1
2
3
4
5
6
7
#
total = 0
 
for student in range(len(students)):
 
    total += students[student].mark
#

Traversing array by values:

1
2
3
4
5
6
7
#
total = 0
 
for student in students:
 
    total += student.mark
#