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()
Maximum, minimum and average
Finding the largest and smallest values in a list are two standard methods that are frequently
found in algorithms. For example, finding the highest and lowest mark
Finding maximum and minimum
MaximumMark 0
MinimumMark 100
FOR Counter 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark StudentMark[Counter]
ENDIF
Finding maximum and minimum without knowing largest and smallest values
MaximumMark StudentMark[1]
MinimumMark StudentMark[1]
FOR Counter 2 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark StudentMark[Counter]
ENDIF
NEXT Counter
Calculate the average (mean)
Total 0
FOR Counter 1 TO ClassSize
Total Total + StudentMark[Counter]
NEXT Counter
Average Total / ClassSize
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
OUTPUT “Please enter name to find”
INPUT Name
Found FALSE
Counter 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found TRUE
ELSE
Counter Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN
OUTPUT Name, “found at position”, Counter, “in the list”
ELSE
Output Name, “not found”
ENDIF
NEXT Counter
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
DECLARE A : ARRAY [1:4] OF INTEGER
DECLARE N, Pass, J, Temp : INTEGER
A [56,100,40,6]
N LENGTH(A)
OUTPUT "Numbers in unsorted order are”, A
FOR Pass 1 TO N
FOR Index 1 TO N-Pass
IF A[Index] > A[Index + 1] THEN
Temp A[Index]
A[Index] A[Index +1]
A[Index + 1] Temp
ENDIF
NEXT Index
NEXT Pass
OUTPUT "Numbers in sorted order are”, A
DECLARE Pass, Index : INTEGER
DECLARE Temp : STRING
FOR Pass 1 TO 999
FOR Index 1 TO 1000-Pass
IF Words[Index] > Words [Index + 1] THEN
Temp Words [Index]
Words [Index] Words [Index +1]
Words [Index + 1] Temp
ENDIF
NEXT Index
NEXT Pass
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
OUTPUT "Enter your name“
INPUT MyName
OUTPUT "Length of your name is :", LENGTH(MyName)
OUTPUT "First 3 characters of your name is :", SUBSTRING(MyName,1,3)
OUTPUT "Your name in upper case is :", UCASE(MyName)
OUTPUT " Your name in lower case is :", LCASE(MyName)