Download presentation

Presentation is loading. Please wait.

Published byMakayla Jolly Modified about 1 year ago

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 !fill array with random numbers For counter = 1 To 20 numbers(counter) = a random number 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 1.Set index to 1 2.Set largest to first number in list 3.Start Do loop 4. If numbers(index) > largest Then 5. largest = numbers(index) 6. End If 7. add 1 to index 8.Loop Until end of list 9.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 1.Set index to 1 2. LET smallest = first number in list 3.Start Do loop 4. If first number in list < smallest Then 5. LET smallest = first number in list 6. End If 7. add 1 to index 8.Loop Until end of list 9.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 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

10
Linear Search Algorithm continued 12. If found Then 13. suitable message Else 14. 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 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 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google