Find Max - Records
Purpose
The purpose of the Find Maximum algorithm are to look through a list of items and find the maximum value or position. It uses a variable to hold the current maximum value or position of the largest value.
If a value is found that is lower than the current highest value or position then that new item is set to the new lowest value or position. This web page will cover them using parallel arrays and also records.
Record Structure
All record examples will use this record structure:
And we have made an array called pupils which contains 40 of these pupil records .
Find Max Pseudocode (Maximum Value)
highest = array[0]
FOR counter FROM 1 to length(pupils)
IF pupils[counter].mark > highest
highest = pupils [counter].mark
END IF
END FOR
You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the highest variable.
Find Max (Maximum Value) Records - Python
In this example we have initialised the maximum value as the mark field from the first pupil in the array of records.
You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the maximum variable.
We only overwrite this value if/when we find a new item that is larger.
Find Max Pseudocode (Position)
Sometimes (particularly when writing functions) it is useful to store the position of the largest value as opposed to the value itself. That way regardless of if we are using parallel arrays or records we can use it to then retrieve other pieces of data.
maxpos = 0
FOR counter FROM 1 to length(pupils)
IF pupils[counter].mark > pupils[maxpos].mark
maxpos = counter
END IF
END FOR
You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the maxpos variable.
Find Max (Maximum Position) Records - Python
This time we have set the maxpos as 0 and only overwrite it with the counter variable when we find a new larger value.
You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the maxpos variable.
This is then returned and can be used outside of the function to find other associated values (in other arrays/records)