Find Max - Parallel Arrays

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. If a value is found that is higher than the current lowest value or position then that new item is set to the new highest value or position. The course will cover this using parallel arrays and also records.

Find Max Pseudocode (Maximum Value)

Parallel Arrays

highest = array[0]

FOR counter FROM 1 to length(array)

IF array[counter] > highest

highest = array [counter]

END IF

END FOR

Find Max  (Maximum Value) Parallel Arrays- Python

In this example we have initialised the maximum value as the first item in the array. 

You will notice that we iterate from the second item in the list (index 1) as we have already used element 0 to initialise the highscore variable.

We only overwrite this value if 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 smallest 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.

Parallel Arrays

maxpos = 0

FOR counter FROM 1 to length(array)

IF array[counter] > array[maxpos] 

maxpos = counter

END IF

END FOR

You will notice that we iterate from the second item in the list (index 1) as we have already used element 0 to initialise the maxpos variable.

Find Max (Maximum Position) Parallel Arrays - Python

This time we have set the highest position as 0 in the variable highpos then only overwrite it with the counter variable when we find a new larger value. 

You will notice that we iterate from the second item in the list (index 1) as we have already used element 0 to initialise the highpos variable.

This is then returned and can be used outside of the function to find other associated values (in other parallel arrays/records)

Find Max Pseudocode with Extra Criteria

Sometimes it will not be possible to set your maximum value as the first item or position as there may be extra criteria. For example you may be looking for the smallest mark from a pupil who is in S1. But what if we look at the data we may see that the first item doesn't fit the criteria we are looking for.

For example in the data below if we initialise our maximum position as 0 it will be initialise the maximum value for an S1 as 50 but that values belongs to an S4 pupil.  The initial maximum should be 45.

If we used the value 50 as our maximum mark for an S1 pupil it will never find the correct initial value of 45.


This new algorithm will first of all look at the value in the first element of the years array and then while it isnt equal to S1 will increment the year counter by 1.

When it eventually does find an element in the year array with a value of S1 it will stop and we can use this as our first initial value. 

Find Min/Max Tutorial Video

4 - Find MinMax.mp4