1 / 15

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

Download Presentation

Standard Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Standard Algorithms Find the highest number

  2. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer

  3. Generate Numbers(uses a fixed loop) Algorithm !fillarray with random numbers • For counter = 1 To 20 • numbers(counter) = a random number 1 - 50 • Print numbers(counter) • Next counter

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

  5. Find The Highest Number Algorithm • 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

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

  7. Find The Lowest Number Algorithm • 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

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

  9. Linear Search Algorithm • Get number_to_look_for from User • Use WHILE loop to validate input • Set found to 0 • Set index to 1 • Start Do loop • If number_to_look_for = numbers(index) Then • set found to 1 • End If • add 1 to index • Loop Until found Or end of list reached

  10. Linear Search Algorithm continued • If found Then • suitable message Else • not found message

  11. 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 !number has not yet been found LET index = 1 ! to start at first number in list continue

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

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

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

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

More Related