Standard Algorithms

Download Report

Transcript Standard Algorithms

Standard Algorithms
Find the highest number
! Your name and today’s date
! Find the maximum
Dim numbers(20) As Integer
Generate Numbers
(uses a fixed loop)
Algorithm
!fill array with random numbers
• For counter = 1 To 20
•
numbers(counter) = a random number 1 - 50
•
Print numbers(counter)
• Next counter
Generate Numbers
(in True Basic)
SUB generate(numbers())
!FILL array with random numbers
Randomize
FOR counter = 1 To 20
LET numbers(counter) = Int((50 * Rnd) + 1)
PRINT numbers(counter)
NEXT counter
END SUB
Find The Highest Number
Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.
Set index to 1
Set largest to first number in list
Start Do loop
If numbers(index) > largest Then
largest = numbers(index)
End If
add 1 to index
Loop Until end of list
Print "The highest number is " ; largest
Find The Highest Number in True Basic
Sub Highest(numbers())
!FIND MAXIMUM
Let index = 1
Let largest = numbers(index)
Do
If numbers(index) > largest Then
let largest = numbers(index)
End If
let index = index + 1
Loop Until index > 20
PRINT "The highest number is " ; largest
End Sub
Find The Lowest Number
Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.
Set index to 1
LET smallest = first number in list
Start Do loop
If first number in list < smallest Then
LET smallest = first number in list
End If
add 1 to index
Loop Until end of list
Print smallest number
Find The Lowest Number in True Basic
Sub Lowest(numbers())
!FIND MINIMUM
LET index = 1
LET smallest = numbers(index)
Do
If numbers(index) < smallest Then
LET smallest = numbers(index)
End If
LET index = index + 1
Loop Until index > 20
PRINT"The lowest number is “; smallest
End Sub
Linear Search
Algorithm
1. Get number_to_look_for from User
2. Use WHILE loop to validate input
3. Set found to 0
4. Set index to 1
5. Start Do loop
6.
If number_to_look_for = numbers(index) Then
7.
set found to 1
8.
End If
9.
add 1 to index
10. Loop Until found Or end of list reached
Linear Search
Algorithm continued
12. If found Then
13. suitable message Else
14. not found message
Linear Search in True Basic
Sub Search(numbers())
!LINEAR SEARCH
Input prompt “What number do you wish to look for “:
number_to_look_for
DO WHILE number_to_look_for < 1 Or number_to_look_for > 50
Input prompt “number not valid – it must be between 1 and 50 “:
number_to_look_for
Loop
LET found = 0
LET index = 1
!number has not yet been found
! to start at first number in list
continue
Linear Search in True Basic continued
Do
If number_to_look_for = numbers(index) Then
LET found = 1
! number found
End If
index = index + 1 ! go to next number in list
Loop Until found = 1 Or index > 20
If found = 1 Then
Print number_to_look_for ; " is in the list"
Else
Print number_to_look_for ; " is NOT in the list"
End If
End Sub
Counting Occurrences Algorithm
Set occurrences to 0
Set index to1
Get number from user and validate
Start Loop
If numbers(index) = number_to_find Then
add 1 to occurrences
End If
add 1 to index
Loop till end of list
Print relevant message
Counting Occurrences in True Basic
Sub count_occurences(numbers())
Let occurrences = 0
Let index = 1
Input prompt "Please enter the number you
wish to find“: number_to_find
Counting Occurrences in True Basic (cont)
Do While number_to_find < 1 OR number_to_find > 50
Input prompt “Invalid data - Please enter a number
between 1 and 50 “: number_to_find
Loop
Do
If numbers(index) = number_to_find Then
occurrences = occurrences + 1
End If
index = index + 1
Loop Until index > 20
Print "The number of times that " ; number_to_find ; "
appeared on the list is " ; occurrences
End Sub