Standard_algorithms

advertisement
Standard Algorithms
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:
• Restrict the data the user can input by only
presenting them with a limited set of possibilities
such as a drop box or pull down menu,
• Accept the input but reject any which does not
meet the restrictions imposed by the software and
ask them to input it again.
This second approach is the one we are going to use.
Input Validation
PROCEDURE GetValidInput()
RECEIVE userInput FROM (INTEGER) KEYBOARD
WHILE userInput < lowerLimit OR userInput >
upperLimit DO
SEND "Input must be between "& lowerLimit & " and
" & upperLimit TO DISPLAY
RECEIVE userInput FROM (INTEGER) KEYBOARD
END WHILE
END PROCEDURE
Input Validation
PROCEDURE GetValidInput()
REPEAT
RECEIVE userInput FROM (INTEGER) KEYBOARD
IF userInput < lowerLimit OR userInput > upperLimit THEN
SEND "Input must be between " & (STRING) lowerLimit & " and "
& (STRING) upperLimit TO DISPLAY
END IF
UNTIL userInput >= lowerLimit AND userInput <= upperLimit
END PROCEDURE
This algorithm is less efficient than the previous one because
the input is being checked twice.
Input validation (String)
PROCEDURE GetValidInput()
SEND ["Please enter Y or N"] TO DISPLAY
RECEIVE userInput FROM (STRING) KEYBOARD
WHILE userInput ≠ ["Y"] AND userInput ≠ ["N"] DO
SEND "Input must be Y or N " TO DISPLAY
RECEIVE userInput FROM (STRING) KEYBOARD
END WHILE
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
In this version of the algorithm the boolean
variable validInput is set to false at the
beginning, and the conditional loop only
terminates once it has been set to true.
This version of the algorithm is useful if you
want to check a number of different
conditions in the input string
Input Validation (Function)
FUNCTION ValidItem(lowerLimit, upperLimit)RETURNS INTEGER
RECEIVE userInput FROM (INTEGER) KEYBOARD
WHILE userInput < lowerLimit OR userInput > upperLimit
DO
SEND "Input must be between "& lowerLimit " and "
&upperLimit" TO DISPLAY
RECEIVE userInput FROM (INTEGER) KEYBOARD
END WHILE
RETURN userInput
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)
or we could call it with the actual parameters 1 and inputRange which is a
variable which has a value assigned elsewhere in the program:
RECEIVE
inputRange FROM (INTEGER) KEYBOARD
numberToUse = ValidItem(1,inputRange)
This call would return a value between 1 and inputRange.
Iterating through arrays
• Finding the maximum / minimum value
• Counting occurrences
• Linear search
Finding the Maximum
SET largest TO the value of the first item in the array
FOR each of the remaining items in the array DO
IF item_value > largest THEN
SET largest TO item_value
END IF
END FOR
print largest
Finding the Maximum
PROCEDURE FindMax()
SET maximumValue TO numbers[0]
FOR counter FROM 1 TO 9 DO
IF maximumValue < number THEN
SET maximumValue TO number
END IF
END FOR
SEND "The largest value was "& (STRING)
maximumValue TO DISPLAY
END PROCEDURE
Finding the Maximum
PROCEDURE FindMax()
SET maximumValue TO numbers[0]
FOR counter FROM 1 TO 9 DO
IF maximumValue < number THEN
SET maximumValue TO number
END IF
END FOR
SEND "The largest value was "& (STRING)
maximumValue TO DISPLAY
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()
SET minimumValue TO numbers[0]
FOR counter FROM 1 TO 9 DO
IF minimumValue > numbers[counter] THEN
SET minimumValue TO numbers[counter]
END IF
END FOR
SEND "The smallest value was " & (STRING)
minimumValue TO DISPLAY
END PROCEDURE
Finding the Minimum
PROCEDURE FindMin()
SET minimumValue TO numbers[0]
FOR counter FROM 1 TO 9 DO
IF minimumValue > numbers[counter] THEN
SET minimumValue TO numbers[counter]
END IF
END FOR
SEND "The smallest value was " & (STRING)
minimumValue TO DISPLAY
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
SET found TO false
SET arraySize TO highestIndex
SET counter TO 0
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
SET found TO false
SET arraySize TO highestIndex
SET counter TO 0
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
SET found TO false
SET arraySize TO highestIndex
SET counter TO 0
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
SET found TO false
SET arraySize TO highestIndex
SET counter TO 0
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
If temp[counter] < min
END FOR
SEND "Maximum = " & max TO
SEND "Minimum = " & min TO
END PROCEDURE
THEN SET max TO temp[counter]
THEN SET min TO temp[counter]
DISPLAY
DISPLAY
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.
The options selected during a day are stored as a list.
The bank would like the software to calculate the
number of times the mobile top-up option appears on
this list. Use pseudocode to design an algorithm to
carry out this calculation.
Example from SQA paper 2011
PROCEDURE countTopups()
SET total TO 0
FOR EACH item FROM options
IF item = mobile_top_up THEN
SET total TO total + 1
END IF
END FOR
SEND "There were "& total&" mobile
topups" to DISPLAY
END PROCEDURE
Another Counting Occurrences
example
• At the end of each round of an archery
competition, those players who have a score which
is the same or greater than the qualifying score are
selected for the next round. The software is
required to count the number of players who have
qualified. Use pseudocode to design an algorithm
to carry out this calculation.
• State the data structure which will be used to store
the players’ scores.
Another Counting Occurrences
example
Data structure is an integer array
PROCEDURE countQualifiers()
SET Number_qualified TO 0
FOR each archer_score FROM scores
IF archer_score >= qualification_score THEN
SET Number_qualified TO Number_qualified +1
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 ID’s 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
Using a design notation with which you are familiar, write an
algorithm that would find a particular customer ID from the
array.
Linear search example
PROCEDURE findEmployee()
SET found TO false
SET counter TO 0
RECEIVE target_ID FROM (STRING) KEYBOARD
REPEAT
SET found TO employee[counter] = target_ID
SET counter TO counter + 1
UNTIL found OR counter > total_employees
IF found THEN
SEND ["Found at position " & counter] TO DISPLAY
ELSE SEND ["Not found"] TO DISPLAY
END IF
END PROCEDURE
Linear search example
PROCEDURE findEmployee()
SET found TO false
SET counter TO 0
RECEIVE target_ID FROM KEYBOARD
REPEAT
SET found TO employee[counter] = target_ID
SET counter TO counter + 1
UNTIL found OR counter > total_employees
IF found THEN
SEND ["Found at position " & counter] TO DISPLAY
ELSE SEND ["Not found"] TO DISPLAY
END IF
END PROCEDURE
Cycle Race: What Data Structure?
•
•
•
•
Race times
Names
Nationalities
Qualifiers
Cycle Race: What Data Structure?
•
•
•
•
Race times:
Names:
Nationalities:
Qualifiers:
real array
string array
string array
boolean array
Cycle Race: What standard algorithm?
•
•
•
•
•
Find the best race time
Find how many cyclists were from Scotland
Find the name of the winner
Find Bradley’s time
Find how many qualified
Best race time
• Best race time
– Find the minimum from race_times array
• Name of winner
– match to names array
• Cyclists from Scotland
– Count occurrences from nationalities array
• Bradley’s time
– Linear search names array then match to race_times array
• Qualifiers
– Count occurrences from qualifiers array
Cycle Race
• The preferred application for this set of
problems is a database – manipulating
arrays is exactly what is happening behind
the scenes in a database application.
Download