Counting Occurences

Counting occurrences is used to count how often a value appears in a list.

General Algorithm

set total to zero
for each item in list
    if item is a mathch then
        add 1 to the counter
    end if
next item

Counting values as they are entered (Nat4)

#
adults = 0
for person in range(10):
    age = int(input("Enter age"))
    if age >= 18:
        adults = adults + 1
#

Counting values is an in an array (Nat5)

#
ages = [18, 12, 15, 21, 19, 23, 21, 35, 25, 14]
#
adults = 0
for person in range(10):
    if ages[person] >= 18:
        adults = adults + 1
#

Adding numbers in an array of records (Higher)

Traversing array by index:

#
adults = 0
for person in range(10):
    if members[person].age >= 18:
        adults = adults + 1
#

Traversing array by values:

#
adults = 0
for person in members:
    if person.age >= 18:
        adults = adults + 1
#