Standard Algorithms [pps]

advertisement
Standard Algorithms
Standard Algorithms
Standard Algorithms
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
Standard Algorithms
What is Input Validation
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.
Standard Algorithms
Input Validation Pseudocode
1
2
3
4
5
Get and store value
Loop WHILE data is out with range
Display error message
Prompt user to re-enter value
End loop
Standard Algorithms
Input Validation VB6.0 code
Number = InputBox (“Enter number between 1 and 10”)
Do While number < min Or number > max
MsgBox (“Must be num between “ & min & ” and “ & max)
Number = InputBox (“Enter number between “ & min & ” and “ & max)
Loop
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.
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.
Standard Algorithms
Linear Search Pseudocode
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 VB6.0 Code
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
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
Standard Algorithms
Counting Occurrences Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Set counter = 0
Get search value
Set pointer to start of the list
Do
If search item = list(position) Then
Add 1 to counter
End If
Move to next position
Until end of list
Display number of occurrences
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())
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.
Standard Algorithms
Maximum Pseudocode
1. Set maximum value to first item in this list
2. Set current position to 1
3. Do
4.
5.
If list(position) > maximum Then
set maximum equal to list(position)
6.
End If
7.
Move to next position
8. Until end of list
9. Display maximum value
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)
Standard Algorithms
Minimum Pseudocode
1. Set minimum value to first item in ths list
2. Set current position to 1
3. Do
4.
5.
If list(position) < minimum Then
set minimum equal to list(position)
6.
End If
7.
Move to next position
8. Until end of list
9. Display minimum value
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)
Standard Algorithm Exam Questions
Lane Time (secs)
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.
1
2
3
4
5
6
7
40.23
41.05
42.88
39.89
40.55
40.01
39.87
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)
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
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.
.
.
.
124
210
105
193
157
Q3. Use pseudocode to design an algorithm
to carry out this calculation (4 marks)
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
Download