Other algorithms

From Higher Computing Science
Jump to: navigation, search

Key points

  • 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.

Information

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 numbersArray adding them all up in the variable total. Each time the FOR EACH loop runs, 1 is added to the variable counter.
  • Therefore, at the end of the loop, the variable counter holds the number of items in the array numberArray, and the variable total holds the total value of all the elements in the array numberArray.

Further information

Test yourself

Teaching resources