standard algorithms. 4 standard algorithms input validation finding the maximum / minimum counting...

Click here to load reader

Post on 16-Dec-2015

219 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Standard Algorithms
  • Slide 2
  • 4 Standard Algorithms Input Validation Finding the Maximum / Minimum Counting Occurrences Linear Search
  • Slide 3
  • Standard Algorithms Other than input validation the other 3 standard algorithms are ways of extracting different pieces of information from an array
  • Slide 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.
  • Slide 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
  • Slide 6 = lowerLimit AND userInput
  • 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 34
  • 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 Using a design notation with which you are familiar, write an algorithm that would find a particular customer ID from the array.
  • Slide 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
  • Slide 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
  • Slide 37
  • Cycle Race: What Data Structure? Race times Names Nationalities Qualifiers
  • Slide 38
  • Cycle Race: What Data Structure? Race times: real array Names: string array Nationalities:string array Qualifiers:boolean array
  • Slide 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 Bradleys time Find how many qualified
  • Slide 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 Bradleys time Linear search names array then match to race_times array Qualifiers Count occurrences from qualifiers array
  • Slide 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.