Find the maximum

From Higher Computing Science
Jump to: navigation, search

Key points

  • Find the maximum is an algorithm that finds the highest number in array. To do this, a variable will be used to store the highest number. Each item in the array will be compared to this number, and if the number in the array is larger than the highest, then the highest will be set to this new number.

Information

Haggis implementation

# Algorithm with position
 SET list TO [1,4,2,5,3]
 SET max TO list[0]
 SET maxpos TO 0
 FOR counter FROM 0 TO 4 DO
   IF list[counter]>max THEN
     SET max TO list[counter]
     SET maxpos TO counter
  END IF
 END FOR
 SEND "maximum is " & (STRING) max & " found at position " & (STRING) maxpos) TO DISPLAY

# Algorithm without position
 SET list TO [1,4,2,5,3]
 SET max TO list[0]
 FOR counter FROM 0 TO 4 DO
   IF list[counter]>max THEN
     SET max TO list[counter]
  END IF
 END FOR
 SEND "maximum is " & (STRING) max TO DISPLAY
 

Python implementation

Videos

Further information

Test yourself

Teaching resources