Standard Methods of Solution
Standard Methods of Solution
Ability to repeat existing methods is very important in the design of algorithms, when an
algorithm is turned into a program the same methods may be repeated many times
Totalling
Totalling means keeping a total that values are added to. For example, keeping a running total
of the marks awarded to each student
Total 0
For Counter 1 TO ClassSize
Total Total + StudentMark[Counter]
NEXT Counter
Counting
Keeping a count of the number of times an action is performed is another standard method.
For example, counting the number of students that passed in the class
PassCount 0
FOR Counter 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50
THEN
PassCount PassCount + 1
NEXT Counter
Count Count + 1
NumberInStock NumberInStock – 1
IF NumberInStock < 20
THEN
CALL Reorder()
Linear Search
A search is used to check if a value is stored in a list
This inspects each item in a list in turn to see if the item matches value searched for
For example, searching for a name in a class list of student names
ChoiceCount 0
FOR Counter 1 TO Length
IF “ice cream” = Dessert[Counter]
THEN
ChoiceCount ChoiceCount + 1
NEXT Counter
OUTPUT ChoiceCount, “chose ice cream as their favourite dessert”
Bubble Sort
An algorithm that makes multiple passes through a list comparing each element with the next
element and swapping them. This continues until there is a pass where no more swaps are
made (ascending, descending, alphabetical)
First 1
Last 10
REPEAT
Swap False
FOR Index First TO Last – 1
IF Temperature[Index] > Temperature[Index + 1]
THEN
Temp Temperature[Index]
Temperature[Index] Temperature[Index + 1]
Temperature[Index + 1] Temp
Swap TRUE
ENDIF
Next Index
Last Last – 1
UNTIL (NOT Swap) OR Last = 1
String Handling
Length – finding the number of characters in the string (spaces counted as characters)
Substring – extracting a part of the string (for example – science from computer science)
Upper – converting all the characters to uppercase
Lower – converting all the characters to lowercase