Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Algorithms. 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search.

Similar presentations


Presentation on theme: "Standard Algorithms. 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search."— Presentation transcript:

1 Standard Algorithms

2 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search

3 Standard Algorithms Other than input validation the other 3 standard algorithms are ways of extracting different pieces of information from an array

4 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.

5 Input Validation PROCEDURE GetValidInput() RECEIVE userInput FROM (INTEGER) KEYBOARD WHILE userInput upperLimit DO SEND "Input must be between "& lowerLimit & " and " & upperLimit TO DISPLAY RECEIVE userInput FROM (INTEGER) KEYBOARD END WHILE END PROCEDURE

6 Input Validation PROCEDURE GetValidInput() REPEAT RECEIVE userInput FROM (INTEGER) KEYBOARD IF 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.

7 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

8 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

9 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

10 Input Validation (Function) FUNCTION ValidItem(lowerLimit, upperLimit)RETURNS INTEGER RECEIVE userInput FROM (INTEGER) KEYBOARD WHILE userInput upperLimit DO SEND "Input must be between "& lowerLimit " and " &upperLimit" TO DISPLAY RECEIVE userInput FROM (INTEGER) KEYBOARD END WHILE RETURN userInput END FUNCTION

11 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.

12 Iterating through arrays Finding the maximum / minimum value Counting occurrences Linear search

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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.

29 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

30 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.

31 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

32 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.

33 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

34 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.

35 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

36 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

37 Cycle Race: What Data Structure? Race times Names Nationalities Qualifiers

38 Cycle Race: What Data Structure? Race times: real array Names: string array Nationalities:string array Qualifiers:boolean array

39 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

40 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

41 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 ppt "Standard Algorithms. 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search."

Similar presentations


Ads by Google