Download presentation

Presentation is loading. Please wait.

Published byLynne Jones Modified over 4 years ago

1
Standard Algorithms

2
Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know about 5 of these algorithms: Input Validation (Int 2) Linear search Counting occurences Finding the maximum value Finding the minimum value

3
Standard Algorithms Input validation checks user input to see if it’s within a valid number range. If it’s outside the valid range the algorithm asks for the number again until it falls within the acceptable range. What is Input Validation

4
Standard Algorithms Input Validation Pseudocode 1Get and store value 2Loop WHILE data is out with range 3 Display error message 4 Prompt user to re-enter value 5End loop

5
Standard Algorithms Input Validation VB6.0 code Number = InputBox (“Enter number between 1 and 10”) Do While number max MsgBox (“Must be num between “ & min & ” and “ & max) Number = InputBox (“Enter number between “ & min & ” and “ & max) Loop

6
Standard Algorithms What is Linear Search? Linear search is the simplest search method to implement and understand. Starting with an array holding 8 numbers with a pointer indicating the first item, the user inputs a search key. Scanning then takes place from left to right until the search key is found, if it exists in the list.

7
Standard Algorithms Linear Search (The search item is 76) Each item is checked to see if it 76, until 76 is found or the last item is checked.

8
1. Set found to false 2. Get search value 3. Start at first element on the list 4. Do while (not end of list) AND (found is false) 5.If current element = search value Then 6.Set found = true 7.Display found message 8.Else 9.Move to next element in the list 10.End If 11. Loop 12. If found = false Then 13.Display not found message 14. End If Standard Algorithms Linear Search Pseudocode

9
IsFound = false SearchValue = InputBox(“Please enter the value your are looking for”) Position = 0 Do While (Position <> UBound(List())) AND (found = false) If List(Position) = SearchValue Then IsFound = true MsgBox(“The item is at position ” & Position & “in the list”) Else Position = Position + 1 End If Loop If found = false Then MsgBox(“The item is not in the list”) End If Standard Algorithms Linear Search VB6.0 Code

10
Standard Algorithms What is Counting Occurrences Programs often have to count occurrences. Examples include counting the number of: students who achieved particular marks in an exam rainfall measurements greater than a particular level words equal to a given search value in a text file. The basic mechanism is simple: 1.a counter is set to 0 2. a list is searched for the occurrence of the search value 3. every time the search value occurs, the counter is incremented

11
Standard Algorithms Counting Occurrences Algorithm 1.Set counter = 0 2.Get search value 3.Set pointer to start of the list 4.Do 5.If search item = list(position) Then 6.Add 1 to counter 7.End If 8.Move to next position 9.Until end of list 10.Display number of occurrences

12
Standard Algorithms Counting Occurrences VB6.0 Code Count = 0 Occurrence = Inputbox(“Please enter value to count”) Position = 0 Do If List(Position) = Occurrence Then Counter = Counter + 1 End If Position = Position + 1 Loop Until Position = UBound(List())

13
Standard Algorithms Maximum And Minimum Algorithms Computers are often used to find maximum and minimum values in a list. For example, a spreadsheet containing running times for videos might make use of a maximum algorithm to identify the video with the longest running time, or a minimum algorithm to identify the shortest running time. To find a maximum, we set up a variable which will hold the value of the largest item that has been found so far, usually the first element. If an element in the array exceeds this working maximum, we give the working maximum that value.

14
Standard Algorithms Maximum Pseudocode 1.Set maximum value to first item in this list 2.Set current position to 1 3.Do 4.If list(position) > maximum Then 5.set maximum equal to list(position) 6.End If 7.Move to next position 8.Until end of list 9.Display maximum value

15
Standard Algorithms Finding the Maximum VB6.0 Code Maximum = List(0) Position = 1 Do If List(Position) > Maximum Then Maximum = List(Position) End If Position = Position + 1 Loop Until Position = UBound(List()) Msgbox(“The maximum value is ” & Maximum)

16
Standard Algorithms Minimum Pseudocode 1.Set minimum value to first item in ths list 2.Set current position to 1 3.Do 4.If list(position) < minimum Then 5.set minimum equal to list(position) 6.End If 7.Move to next position 8.Until end of list 9.Display minimum value

17
Standard Algorithms Finding the Minimum VB6.0 Code Minimum = List(0) Position = 1 Do If List(Position) < Minimum Then Minimum = List(Position) End If Position = Position + 1 Loop Until Position = UBound(List()) Msgbox(“The minimum value is ” & Minimum)

18
Standard Algorithm Exam Questions An international athletics competition between eight countries has a number of events. The winning times are stored in a list in order of lane number like the one on the right. The stadium needs a program to help process the results. Q1.The program must find the fastest time for a race. Use pseudocode to design an algorithm to find the fastest time (4 marks) Q2. It is suggested that algorithm should find the lane number of the fastest time instead of the fastest time. Explain how this could be achieved. (1 mark) Lane Time (secs) 140.23 241.05 342.88 439.89 540.55 640.01 739.87

19
Exam Marking Scheme Set fastest to first time in list For rest of array items If array(current)<fastest then Set fastest to array(current) End if End loop In summary, 1 mark for each of the following: Setting initial value Loop (with end) for traversal of array Comparison of current element with maximum value (with end if) Assignment of new maximum value

20
Standard Algorithm Exam Questions NoTow is a company that runs a city centre car park. The company requires a piece of software that will calculate the number of cars on a particular day that spent more than three hours in the car park. The number of whole minutes each car is parked is stored in a list as shown on the right. Q3. Use pseudocode to design an algorithm to carry out this calculation (4 marks). 124 210 105 193 157

21
Exam Marking Scheme Set over3 = 0 For each car that day If duration >180 then Add one to over3 End if End loop 1 mark for initialising 1 mark loop with termination 1 mark for if..endif with correct condition 1 mark for keeping running total Note: End of if/loop may be implicit in clearly indented algorithm The value is in minutes so the condition is > 180

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google