From Higher Computing Science
- In Higher, you are also expected to be able to analyse and comment on other algorithms of similar complexity to the ones you have already studied.
- In the exam, these algorithms will be written in the SQA's reference language, previously called Haggis.
An example of such an algorithm could be as follows:
SET numberArray TO [3,1,4,1,5,9,2,6] SET total TO 0 SET counter TO 0 FOR EACH number FROM numberArray DO SET total TO total + number SET counter TO counter + 1 END FOR SET average TO total / counter
- The above algorithm calculates the average of a set of numbers in an array. This algorithm would be considered of similar complexity to the ones you have been taught (input validation, etc), and as such you should be able to work out how it works.
- Essentially is loops through each item in the array
numbersArrayadding them all up in the variable
total. Each time the
FOR EACHloop runs, 1 is added to the variable
- Therefore, at the end of the loop, the variable
counterholds the number of items in the array
numberArray, and the variable
totalholds the total value of all the elements in the array