The counting occurrences algorithm looks through a list of items and counts the number of times (occurrences) that a match is found. It again checks every item in the list and increments a counter when a match is found. It is important to only output the amount of matches after the entire list has been traversed.Â
The algorithm is just a basic linear search but using a variable to keep a running total.
All record examples will use this record structure:
And we have made an array called pupils which contains 40 of these pupil records .
SearchName = Input from user
Total = 0
FOR counter = 0 TO Length(array).name
IF array(counter).name = SearchNameÂ
total = total +1
END IF
END FOR
It is important to initialise the variable occurrences to 0 before beginning.
This could be used within a function to return the amount of occurrences ( which may be 0)
Reusing a function when using records can be more problematic when using records as there is no way to pass in only a particular set of fields.Â
One way around this is to pass in an additional parameter that can help specify what you are looking for in the array of records.