Download presentation
Presentation is loading. Please wait.
Published byRosamond Floyd Modified over 8 years ago
1
Marr CollegeHigher Software DevelopmentSlide 1 Higher Computing Software Development Topic 4: Standard Algorithms
2
Marr CollegeHigher Software DevelopmentSlide 2 Input Validation Algorithm
3
Marr CollegeHigher Software DevelopmentSlide 3 It is essential that software programs minimise the chance of users entering unwanted (exceptional) data either by mistake or deliberately as this affect a program’s reliability and robustness. Activity Can you think of three situations where you have entered incorrect data into a computer and you have been asked to re-enter? Answer Typing a telephone number into a Postcode field Typing an incorrect PIN number Typing a web address incorrectly Standard Algorithms : Validating user input
4
Marr CollegeHigher Software DevelopmentSlide 4 Standard Algorithms The Input Validation is a classic standard algorithm that prevents the user entering exceptional (unwanted) data into a program. You need to be able to: Describe how the algorithm works Write the pseudocode for the algorithm Write VB6 code for the algorithm Standard Algorithms : Validating user input
5
Marr CollegeHigher Software DevelopmentSlide 5 VB6 CodePseudocode The INPUT VALIDATION checks program input and rejects invalid data. Standard Algorithms 1.1Get and store value 1.2Loop WHILE data is out with range 1.3 Display error message 1.4 Prompt user to re-enter value 1.5End loop num = InputBox (“Enter number between 1 and 10”) Do While num 10 MsgBox “Must be num between 1 and 10” Num = InputBox (“Enter number between 1 and 10”) Loop Standard Algorithms : Validating user input
6
Marr CollegeHigher Software DevelopmentSlide 6 Standard Algorithms Input Validation Tasks Write a program or button that does each of the following: 1.Prompt the user to enter their age. Do not accept ages less than 0 or greater than 120 as valid ages. 2.Prompt the user to enter a 4 digit PIN. The program should only accept the PIN if it is four digits long. (Hint: make it a string and use the Len function) 3.Prompt the user to enter what year they are in at school. Accept only 1, 2, 3, 4, 5 or 6 as valid years. 4.Prompt the user to enter their type of membership in a club. Membership codes are J (for Junior), I (for Intermediate) and S (for Senior) 5.Prompt the user to enter Yes or No. The program should accept “YES”, “Yes”, “yes”, “NO”, “No” or “no”. 6.Prompt the user to enter a name. The program should only accept a name beginning with the letter “A”. (Hint: use Mid$) Standard Algorithms : Validating user input
7
Marr CollegeHigher Software DevelopmentSlide 7 Linear Search Algorithm
8
Marr CollegeHigher Software DevelopmentSlide 8 Standard Algorithms : Searching Being able to search for data is one of the main reason computers and mobile devices have become so popular…. Activity Can you think of three situations where you have made use of a computer or mobile device to search for something? Answer Searching for a telephone number Searching for a product e.g. Argos catalogue Searching for an image on Google images
9
Marr CollegeHigher Software DevelopmentSlide 9 Standard Algorithms The linear search is a classic standard algorithm that searches a list to see if a value is present or not…. You need to be able to: Recognise the use of a linear search (Int 2) Describe how it works Write the pseudocode for the algorithm Write VB6 code for the algorithm Standard Algorithms : Searching
10
Marr CollegeHigher Software DevelopmentSlide 10 Standard Algorithms Here is an unsorted list of integers: 213562-953 (1)(2)(3)(4)(5)(6)(7) Standard Algorithms : Searching
11
Marr CollegeHigher Software DevelopmentSlide 11 Standard Algorithms Ask the user to enter the number they wish to search for… User enters -9 213562-953 (1)(2)(3)(4)(5)(6)(7) Standard Algorithms : Searching
12
Marr CollegeHigher Software DevelopmentSlide 12 Standard Algorithms Comparison 1 Ask…does the value in position 1 equal -9??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Searching
13
Marr CollegeHigher Software DevelopmentSlide 13 Standard Algorithms Comparison 2 Ask…does the value in position 2 equal -9??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Searching
14
Marr CollegeHigher Software DevelopmentSlide 14 Standard Algorithms Comparison 3 Ask…does the value in position 3 equal -9??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Searching
15
Marr CollegeHigher Software DevelopmentSlide 15 Standard Algorithms Comparison 4 Ask…does the value in position 4 equal -9??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Searching
16
Marr CollegeHigher Software DevelopmentSlide 16 Standard Algorithms Comparison 5 Ask…does the value in position 5 equal -9??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If yes….then display item and its location in the list... -9 was found at position 5 in the list! Standard Algorithms : Searching
17
Marr CollegeHigher Software DevelopmentSlide 17 Standard Algorithms Activity Write an algorithm to search a list of numbers for a target number. The list should contain: 12, 9, 21, 16, 4, 3, 17 Solution 1.Ask for the number you wish to search for 2.Go to the first or next item in the list 3.If the number is the one you are looking for then 4.You have found it! 5.Repeat steps 2, 3 and 4 until the end of the list 6.If not in the list then not found! Standard Algorithms : Searching
18
Marr CollegeHigher Software DevelopmentSlide 18 The LINEAR SEARCH searches a list for a target value. Standard Algorithms Pseudocode 2.1Get and store target value 2.2Loop for each item in the list 2.3 If current item = target value THEN 2.4 Display item and its location in list 2.5 Exit the loop 2.5 End if 2.6End loop Standard Algorithms : Searching
19
Marr CollegeHigher Software DevelopmentSlide 19 The LINEAR SEARCH searches a list for a target value. Standard Algorithms VB6 Code target = InputBox (“Enter target value”) For p = 1 To 10 If nums(p) = target Then lblMsg.caption = target & “found at position “ & p Exit For End If Next p Standard Algorithms : Searching
20
Marr CollegeHigher Software DevelopmentSlide 20 Standard Algorithms Linear Search Tasks Write a program that: 1.Lets the user search a list of 10 whole numbers. 2.Lets the user search a list of 7 names. 3.Lets the user search a list of 31 temperatures. Hint: Setup an array of 31 numbers as single and generate the temperatures randomly i.e. For i = 1 to 31 temperatures(i) = Format((rnd*35),”0.0”) Next i Extension What happens if the number is not in the list….??? Standard Algorithms : Searching
21
Marr CollegeHigher Software DevelopmentSlide 21 Counting Occurrences Algorithm
22
Marr CollegeHigher Software DevelopmentSlide 22 In Mathematics we learn that Frequency is the number of times a particular value occurs in a set of data. We would record the number of occurrences of an item of data in a Frequency Table. Standard Algorithms Standard Algorithms : Counting Occurrences Example Pupils in a Science class sit a class test and get marks out of 10. Frequency Table
23
Marr CollegeHigher Software DevelopmentSlide 23 There are many situations today where computing power can be used to help perform mathematical tasks – known as computations (hence the name computer!!). Like searching, a common task is to speed up the counting of occurrences of data items in memory, lists and databases. Activity Can you think of three situations where computers could be used to count occurrences of data items that would speed up a person doing the counting manually? Answer Identifying the number of pupils attaining an ‘A’ award for Higher English in Scotland The number of days ‘since records began’ where the temperature was greater than 30 0 C The number of hits to a website at certain times of the day Standard Algorithms : Counting Occurrences
24
Marr CollegeHigher Software DevelopmentSlide 24 A variation of the linear search is the Counting Occurrences algorithm. It searches through a list and instead of stopping when it finds the value it is looking for, it runs through to the end of the list counting up each occurrence of the target value. Standard Algorithms Standard Algorithms : Counting Occurrences You need to be able to: Recognise the use of counting occurrences (Int 2) Describe how it works Write the pseudocode for the algorithm Write VB6 code for the algorithm
25
Marr CollegeHigher Software DevelopmentSlide 25 Standard Algorithms Here is the same unsorted list of integers used to demonstrate the linear search: 213562-953 (1)(2)(3)(4)(5)(6)(7) Standard Algorithms : Counting Occurrences
26
Marr CollegeHigher Software DevelopmentSlide 26 Standard Algorithms Ask the user to enter the number they wish to count occurrences of… User enters 3 213562-953 (1)(2)(3)(4)(5)(6)(7) Standard Algorithms : Counting Occurrences
27
Marr CollegeHigher Software DevelopmentSlide 27 Standard Algorithms Comparison 1 Ask…does the value in position 1 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Counting Occurrences tally = 0
28
Marr CollegeHigher Software DevelopmentSlide 28 Standard Algorithms Comparison 2 Ask…does the value in position 2 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If yes….then add 1 to the tally. Standard Algorithms : Searching tally = 1
29
Marr CollegeHigher Software DevelopmentSlide 29 Standard Algorithms Comparison 3 Ask…does the value in position 3 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Counting Occurrences tally = 1
30
Marr CollegeHigher Software DevelopmentSlide 30 Standard Algorithms Comparison 4 Ask…does the value in position 4 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Counting Occurrences tally = 1
31
Marr CollegeHigher Software DevelopmentSlide 31 Standard Algorithms Comparison 5 Ask…does the value in position 5 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Counting Occurrences tally = 1
32
Marr CollegeHigher Software DevelopmentSlide 32 Standard Algorithms Comparison 6 Ask…does the value in position 6 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) If no….then move to next position. Standard Algorithms : Counting Occurrences tally = 1
33
Marr CollegeHigher Software DevelopmentSlide 33 Standard Algorithms Comparison 7 Ask…does the value in position 7 equal 3??? 213562-953 (1)(2)(3)(4)(5)(6)(7) Standard Algorithms : Counting Occurrences If yes….then add 1 to the tally. As end of list display.... The number 3 appears 2 times so the frequency of 3 is 2. tally = 2
34
Marr CollegeHigher Software DevelopmentSlide 34 Standard Algorithms Activity Write an algorithm to search a list of numbers for a target number and ‘tally up’ the number of times the number appears in the list. The list should contain: 12, 9, 21, 9, 4, 3, 17, 21, 12, 12 Solution 1.What number do you wish to tally up? 2.Go to the first or next item in the list 3.If the number is the one you are looking for then 4.Add 1 to the tally 5.Repeat steps 2, 3 and 4 until the end of the list 6.If not in the list then not found! Standard Algorithms : Counting Occurrences
35
Marr CollegeHigher Software DevelopmentSlide 35 The COUNTING OCCURRENCES algorithm searches a list for a target value and totals up the number of times it appears in the list. Standard Algorithms Pseudocode 2.1Set tally equal to 0 2.2 Get and store target value 2.3Loop for each item in the list 2.4 If current item = target value THEN 2.5 Add 1 to the tally *** 2.6 End if 2.7End loop 2.8Display tally Standard Algorithms : Counting Occurrences *** Remember...this is really just the same as the LINEAR SEARCH but with a counter variable to tally up!!!
36
Marr CollegeHigher Software DevelopmentSlide 36 Standard Algorithms VB6 Code tally = 0 target = InputBox (“Enter target value”) For i = 1 To 10 If nums(i) = target Then tally = tally + 1 End If Next i lblMsg.caption = target & “ appears in the list “ & tally & “ times so the frequency of “ & target & “ is “ & tally & “!” The COUNTING OCCURRENCES algorithm searches a list for a target value and totals up the number of times it appears in the list. Standard Algorithms : Counting Occurrences
37
Marr CollegeHigher Software DevelopmentSlide 37 Standard Algorithms Counting Occurrences Tasks Write a program that: 1.Counts the number of times a number appears in a list of 10 whole numbers. 2.Counts the number of times a name appears in a list 7 names. 3.Counts the number of times a temperature appears in a list of 31 temperatures. Hint: Setup an array of 31 numbers as single and generate the temperatures randomly i.e. For i = 1 to 31 temperatures(i) = Format((rnd*35),”0.0”) Next i Extension What happens if the number is not in the list….??? Standard Algorithms : Counting Occurrences
38
Marr CollegeHigher Software DevelopmentSlide 38 Standard Algorithms Programming Challenge A Science class of 30 pupils sit a class test and are awarded marks out of 10. Write a program that outputs the frequency table of results using the following data: Standard Algorithms : Counting Occurrences How will the program repeatedly search for each mark from 1 to 10? Hint: Try using a loop that loops for each mark and another loop within that loops for each item in the list to tally up the matches.... The tricky bit....
39
Marr CollegeHigher Software DevelopmentSlide 39 Finding Minimum & Maximum Algorithm
40
Marr CollegeHigher Software DevelopmentSlide 40 Pseudocode Find largest value in a list. Standard Algorithms 4.1 Set max = first item 4.2 Loop for each remaining item in the list 4.3 If current item > max then 4.4 Set max = current item 4.5 Set position = counter 4.6 End if 4.7 End loop VB6 Code max = array(1) For p = 2 To 10 If array(p) > max Then max = array(p) pos = p End If Next p Standard Algorithms : Finding Maximum
41
Marr CollegeHigher Software DevelopmentSlide 41 Standard Algorithms Often it is very use to find the smallest number in a list. Standard Algorithms : Finding Maximum
42
Marr CollegeHigher Software DevelopmentSlide 42 5 Finding Minimum Standard Algorithms Set min to first item in list… 213562-953 (1)(2)(3)(4)(5)(6)(7) min
43
Marr CollegeHigher Software DevelopmentSlide 43 5 Finding Minimum Standard Algorithms Is 3 less than 21? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
44
Marr CollegeHigher Software DevelopmentSlide 44 5 Finding Minimum Standard Algorithms If yes then set min as 3. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
45
Marr CollegeHigher Software DevelopmentSlide 45 5 Finding Minimum Standard Algorithms Is 56 less than 3? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
46
Marr CollegeHigher Software DevelopmentSlide 46 5 Finding Minimum Standard Algorithms If no then do nothing. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
47
Marr CollegeHigher Software DevelopmentSlide 47 5 Finding Minimum Standard Algorithms Is 2 less than 3? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
48
Marr CollegeHigher Software DevelopmentSlide 48 5 Finding Minimum Standard Algorithms If yes then set 2 equal to min. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
49
Marr CollegeHigher Software DevelopmentSlide 49 5 Finding Minimum Standard Algorithms Is -9 less than 2? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
50
Marr CollegeHigher Software DevelopmentSlide 50 5 Finding Minimum Standard Algorithms If yes then set min equal to -9. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
51
Marr CollegeHigher Software DevelopmentSlide 51 5 Finding Minimum Standard Algorithms Is 5 less than -9? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
52
Marr CollegeHigher Software DevelopmentSlide 52 5 Finding Minimum Standard Algorithms If no then do nothing. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
53
Marr CollegeHigher Software DevelopmentSlide 53 5 Finding Minimum Standard Algorithms Is 3 less than -9? 213562-953 (1)(2)(3)(4)(5)(6)(7) min
54
Marr CollegeHigher Software DevelopmentSlide 54 5 Finding Minimum Standard Algorithms If no then do nothing. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
55
Marr CollegeHigher Software DevelopmentSlide 55 5 Finding Minimum Standard Algorithms -9 is the smallest value in the list. 213562-953 (1)(2)(3)(4)(5)(6)(7) min
56
Marr CollegeHigher Software DevelopmentSlide 56 Pseudocode 5 Finding Minimum Find smallest value in a list. Standard Algorithms 5.1 Set min = first item 5.2 Loop for each remaining item in the list 5.3 If current item < min then 5.4 Set min = current item 5.5 Set position = counter 5.6 End if 5.7 End loop VB6 Code min = array(1) For p = 2 To 10 If array(p) < min Then min = array(p) pos = p End If Next p
57
Marr CollegeHigher Software DevelopmentSlide 57 TASK – Find Min and Max Write a program that will: a)allow the user to input 10 scores between 1 and 6. The scores can be real i.e. 2.3, 5.6 etc. b)Calculate the total score i.e. each of the scores totalled up! c)Find the minimum and maximum score and subtract from the overall total to obtain a final score. Hint: final = total – (min + max) Use a function! d)Display all the scores on screen, along with the minimum, the maximum and the final score. e)Extension: Adapt the program to display each score on screen in ascending (sorted) order as it is entered by the user. Have fun!!! McLean Standard Algorithms
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.