Algorithm - Maximum and minimum in array
The idea behind this algorithm is to read the array sequentially once (i.e. make one pass or traverse through the array) and find (or return) the required element in a variable. As we read the array we store the largest (or smallest) element encountered so far in a variable. Call it maxval (or minval). We compare the value in maxval with the element in each other index position we read. If the element in the new index position is larger than the value in maxval it becomes our new maximum value by overwriting the value in maxval with the new maximum. When we have finished reading the file, comparing the elements to the maximum value encountered so far and overwriting maxval, as necessary, the final value in maxval will be the largest value (or element) in the array.
Algorithm
BEGIN FINDMAX
highindex = ARRAYLENGTH(element)
index = 1
maxval = element(index)
WHILE index <= highindex
IF element(index) > maxval
THEN maxval = element(index)
ENDIF
index = index + 1
ENDWHILE
DISPLAY "The maximum element is " maxval
END FINDMAX
ACTIVITY 2
Complete the algorithm worksheet and submit here.