includig binary and multiway
Pre-test, post-test and for-next loops
used to make a mainline simpler to read and follow!
Algorithms can be used to do many thing, including:
The Syllabus support document (found here on NESA's website) has a standard algorithms to load an array and print its contents. These arrays are:
Load an array:
BEGIN LoadArray
Let i = 1
Read DataValue
WHILE DataValue < > “xxx”
Let Element (i) = DataValue
i = i + 1
Read DataValue
ENDWHILE
Let NumElements = i
Display “ There are” NumElements “ items loaded into the array”
END LoadArray
and to print it:
BEGIN PrintArrayContents
Let i = 1
REPEAT
Display Element (i)
i = i + 1
UNTIL i > = NumElements
END PrintArrayContents
Note that if the number of elements is not known, the sentinel value would be stored into the last element in the array, and printing would continue until the sentinel value of “xxx” is encountered.
source: Software Design and Development syllabus support document, updated 2013
Tasks: Can you:
Create a flowchart of these algorithms?
Create a testing table of these algorithms
The following arrays are printed in the syllabus support document demonstrating how an algorithm can be used to add the content of an array of numbers.
BEGIN SumArrayContents
Let i = 1
Let total = 0
REPEAT
Let total = Element (i) + total
i = i + 1
UNTIL i > NumElements
Display “ The sum of all of the elements in the array = ” total
END SumArrayContents
note that this is a post test loop. A pre-tested version, using a sentinal value of 999 can also be applied.
BEGIN SumArrayContents
Let i = 1
Let total = 0
WHILE Element (i) < > 999
Let total = Element (i) + total
i = i + 1
END WHILE
Display “There are ” i “ elements”
Display “The sum of all of the elements in the array = ” total
END SumArrayContents
Note that the sentinal is not added into the total.
source: Software Design and Development syllabus support document, updated 2013
We use structured algorithms for: