Beruflich Dokumente
Kultur Dokumente
4 Standard Algorithms
Input Validation
Finding the Maximum / Minimum
Counting Occurrences
Linear Search
Standard Algorithms
Other than input validation the other 3
standard algorithms are ways of extracting
different pieces of information from an
array
Input Validation
Input validation can be achieved in two ways:
END PROCEDURE
Input Validation
PROCEDURE GetValidInput()
REPEAT
END PROCEDURE
END PROCEDURE
Input Validation with boolean flag
PROCEDURE GetValidInput()
validInput = false
REPEAT
RECEIVE userInput FROM (STRING) KEYBOARD
IF length(userInput) < lengthLimit THEN
validInput = true
ELSE SEND "Input must be less than " &
(STRING) lengthLimit & " characters" TO DISPLAY
END IF
UNTIL validInput = true
END PROCEDURE
Input Validation with boolean flag
END FUNCTION
Input Validation (Function)
We could call this function with actual parameters, 1 and 50 to return a number
between 1 and 50:
numberToUse = ValidItem(1,50)
END PROCEDURE
Finding the Maximum
PROCEDURE FindMax()
END PROCEDURE
Finding the Minimum
SET smallest TO the value of the first item in the array
FOR each of the remaining items in the array
IF item_value < smallest THEN
SET smallest TO item_value
END IF
END FOR
print smallest
Finding the Minimum
PROCEDURE FindMin()
END PROCEDURE
Finding the Minimum
PROCEDURE FindMin()
END PROCEDURE
Counting Occurrences
Ask for target_value to count
SET number_found TO zero
FOR every item in the array
IF equal to target_value THEN add 1 to number_found
END FOR
Print number_found
Counting Occurrences
PROCEDURE CountOccurrences()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
SET numberFound TO 0
FOR EACH number FROM numbers DO
IF number = itemToFind THEN
SET numberFound TO numberFound + 1
END IF
END FOREACH
SEND "There were " & (STRING) numberFound) & "occurrences of
" & (STRING) itemToFind & " in the list" TO DISPLAY
END PROCEDURE
Counting Occurrences
PROCEDURE CountOccurrences()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
SET numberFound TO 0
FOR EACH number FROM numbers DO
IF number = itemToFind THEN
SET numberFound TO numberFound + 1
END IF
END FOREACH
SEND "There were " & (STRING) numberFound) & "occurrences of
" & (STRING) itemToFind & " in the list" TO DISPLAY
END PROCEDURE
Counting Occurrences
PROCEDURE CountOccurrences()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
SET numberFound TO 0
FOR counter FROM 0 TO 9 DO
IF number[counter] = itemToFind THEN
SET numberFound TO numberFound + 1
END IF
END FOR
SEND "There were " & (STRING) numberFound) & "occurrences of "
& (STRING) itemToFind & " in the list" TO DISPLAY
END PROCEDURE
Linear Search
Input target_value from user
SET found to false
SET Counter TO 0
REPEAT
IF array[counter] equals target_value THEN SET found to
true
END IF
add 1 to counter
UNTIL found
IF Counter > list_length THEN print "Not found"
Print "Found at position" counter
Linear Search
PROCEDURE linearSearch()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
REPEAT
IF numbers[counter] = itemToFind THEN
SET found to true
END IF
SET counter TO counter + 1
UNTIL found OR counter > arraySize
IF found THEN
SEND (STRING) itemToFind &" found at position" & (STRING)
counter - 1) TO DISPLAY
ELSE SEND "Item not found" TO DISPLAY
END IF
END PROCEDURE
Linear Search
PROCEDURE linearSearch()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
REPEAT
IF numbers[counter] = itemToFind THEN
SET found to true
END IF
SET counter TO counter + 1
UNTIL found OR counter > arraySize
IF found THEN
SEND (STRING) itemToFind &" found at position" & (STRING)
counter - 1) TO DISPLAY
ELSE SEND "Item not found" TO DISPLAY
END IF
END PROCEDURE
Linear Search
PROCEDURE linearSearch()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
REPEAT
IF numbers[counter] = itemToFind THEN
SET found to true
END IF
SET counter TO counter + 1
UNTIL found OR counter > arraySize
IF found THEN
SEND (STRING) itemToFind &" found at position" & (STRING)
counter - 1) TO DISPLAY
ELSE SEND "Item not found" TO DISPLAY
END IF
END PROCEDURE
Linear Search
PROCEDURE linearSearch()
RECEIVE itemToFind FROM (INTEGER) KEYBOARD
REPEAT
SET found TO numbers[counter] = itemToFind
SET counter TO counter + 1
UNTIL found OR counter > arraySize
IF found THEN
SEND (STRING) itemToFind &" found at position" & (STRING)
counter - 1) TO DISPLAY
ELSE SEND "Item not found" TO DISPLAY
END IF
END PROCEDURE
Example from SQA paper 2010
A subprogram in building management
software is used to find the range of
temperatures in a building in one day. The
temperature is recorded every 15 minutes
within a 24 hour period and stored in a list.
Use pseudocode to design one algorithm to
find both the highest and lowest temperatures
in this list.
Example from SQA paper 2010
PROCEDURE findMaxMin()
SET min TO temp[0]
SET max TO temp[0]
FOR counter FROM 0 TO 95
If temp[counter] > max THEN SET max TO temp[counter]
If temp[counter] < min THEN SET min TO temp[counter]
END FOR
SEND "Maximum = " & max TO DISPLAY
SEND "Minimum = " & min TO DISPLAY
END PROCEDURE
Example from SQA paper 2011
RightIT, a software company, is currently developing
a cash machine program for a bank. The cash
machine will offer five options to customers.
ENDIF
END FOR
SEND "Number of qualifiers = "& Number_qualified TO
DISPLAY
END PROCEDURE
Linear search example
Customers are given a unique ID number when they register
with their employer. Customer IDs are created using the first
3 letters of the customers surname and the last 3 letters of
their postcode. This ID is stored in a 1-D array.
For example: Mr J Brown with a postcode of EH15 6NW
has a customer ID of BRO6NW
Race times
Names
Nationalities
Qualifiers
Cycle Race: What Data Structure?