Higher Computing. Software Development. LO4 Standard Algorithms

Higher Computing Software Development LO4 – Standard Algorithms Ian Simpson Inverurie Academy 2006 Higher Computing Software Development The candid...
6 downloads 2 Views 352KB Size
Higher Computing Software Development LO4 – Standard Algorithms

Ian Simpson Inverurie Academy 2006

Higher Computing Software Development The candidate must demonstrate knowledge and understanding, practical skills and problem solving based on the following content statements: Standard algorithms •

Description and exemplification of the following standard algorithms in pseudocode and an appropriate high level language: o linear search o counting occurrences o finding min/max

ISK 2006

Input Validation

L04

Task A1: Test Scores Write a program which asks the user to enter a test score between 0 and 100 then displays it in a list.

Main Algorithm: 1. 2. 3. 4. 5. 6.

get test score from user WHILE test score is less than 0 OR greater than 100 prompt user to try again get test score from user ENDWHILE add score to list

Visual Basic Code: test_score = InputBox("Enter test score (0-100)") While test_score < 0 Or test_score > 100 MsgBox ("Invalid value - try again please") test_score = InputBox("Enter test score (0-100)") Wend

Tasks: •

Create the following form, ensuring your name and the current date are displayed in the label lblCredits:

Command Button cmdStart

List Box lstOutput Label lblCredits



Enter the Visual Basic code given above within the cmdStart_Click subroutine.



Add one line of code which will add the accepted test score to a list box called lstOutput.



Save this program as Task_A1.

ISK 2006

1

Input Validation

L04

Task A2: Talent Show Write a program which asks the user to input their name and age. If the age is between 11 and 18, they are allowed to enter the school talent show and an appropriate personalised message should be displayed.

Main Algorithm: 1. 2. 3. 4. 5. 6. 7.

get name from user get age from user WHILE age is less than 11 OR greater than 18 prompt user to try again get age from user ENDWHILE display personalised message allowing user to enter talent show

Tasks: x Command Button

Start

by ,

Label



Create a suitable form from the design above:



Convert the above algorithm to Visual Basic code and enter this code in the command button _Click subroutine.



Save program as Task_A2.

Task A3: School Houses Write a program which asks the user to enter their name and the school house they belong to. Acceptible data for the school house is either Barra, Crichie or Davah – any other input should be rejected. Once a valid school house has been entered the program should add the user’s name and school house to a list.

Tasks: • • •

Write down the main algorithm to solve this problem. Design a suitable HCI. Print out your design. Let your teacher check this before proceeding further.

• • •

Implement your design using Visual Basic. Save program as Task_A3. Print out the source code and a screenshot of your completed program.

ISK 2006

2

Linear Search

L04

Task B1: Colours Basic Linear Search Write a program which displays a list of colours and asks the user to enter the colour of their choice. The program will then attempt to find the colour in the list and display its position. No message is to be displayed if a match is not found.

Main Algorithm:

Visual Basic Code:

1. 2. 3. 4. 5. 6. 7. 8.

call set_up_colour_list choice = InputBox(“Enter your colour choice”) FOR counter = 0 to (no_colours-1) IF colour(counter) = choice THEN MsgBox(“Match found at position “ & counter)

set up colour list get colour choice from user FOR each item in the list IF item value = target value THEN report that a match has been found report position of matching value in list ENDIF NEXT item in list

ENDIF NEXT counter

Tasks: •

Create the following form, ensuring your name and the current date are displayed in the label lblCredits:

List Box lstColour

Command Button cmdStart Label lblCredits



Enter the Visual Basic code given above within the cmdStart_Click subroutine.



Add the following Visual Basic code above the cmdStart_Click subroutine to define the variables used in the program.

OPTION EXPLICIT ‘This means all variables must be defined before use CONST no_colours = 4 DIM colour(0 to no_colours-1) as String DIM counter as Integer DIM choice as String

ISK 2006

1

Linear Search •

L04

Create the subroutine set_up_colour_list using the Visual Basic below.

Private Sub set_up_colour_list() ‘ set up colour list colour(0) = “Blue” colour(1) = “Red” colour(2) = “Green” colour(3) = “Purple” lstColour.Clear FOR counter = 0 to (no_colours-1) lstColour.AddItem(colour(counter)) NEXT counter End Sub •

Save program as Task_B1.



Run the program. Does it work as expected? Complete the table below:

User Input – Normal Data Test Expected Actual Data Result Result

Red

Found – pos 1

Green Found – pos 2

OK?

User Input – Extreme Data Test Expected Actual Data Result Result

Purple Found – pos 3

Blue

Found – pos 0

OK?

User Input – Exceptional Data Test Expected Actual OK? Data Result Result

White No message = not found red No message = not found



There is one line of Visual Basic code which allows you to expand the number of colours available for searching. Write out this line below.



Expand the program so that it can handle 8 colours (as well as changing the above line, you will have to set up the extra colours).



Save your project as Task_B1b. Save your form as frmTask_B1b.

ISK 2006

2

Linear Search

L04

Task B2: Pupil List Linear Search with no match flag Write a program which allows the user to search for a specific pupil in a list: if the pupil is in the list their position should be displayed on the screen, if the pupil is not found in the list an appropriate message should be displayed.

Main Algorithm: 1. set up pupil list 2. get name of pupil to search for from user 3. set no match flag to TRUE 4. FOR each pupil in list 5. IF pupil name = search name THEN 6. report that a match has been found in pupil list 7. report position of matching value in list 8. set no match flag to FALSE 9. ENDIF 10. NEXT pupil in list 11. IF no match flag is still TRUE THEN 12. report no match found in pupil list 13. ENDIF

Tasks: x

Linear Search Task B2 – Pupil List

Command Button

List Box

Start

by ,

Label



Create a suitable form from the design above:



Using Task B1 as a guide, convert the above algorithm to Visual Basic code and enter this code in the command button _Click subroutine. Remember to define the variables used in the program and write code for any additional subroutines you may need.



In Microsoft Word, create 3 test data tables exemplified in Task B1 (Normal, Extreme and Exceptional) and enter two suitable user inputs for each table.



Complete the table and print it out with your name as a footer. If any test cases fail, debug your program and re-test.



Save program as Task_B2.



Print out the program source code with your name as a comment.

ISK 2006

3

Linear Search

L04

Task B3: 21st Century Santa Claus Advanced multiple Linear Search with no match flag and Input Validation Write a program which allows Santa to search his naughty AND nice list for a specific name. If the name is found in the naughty or nice list an appropriate message should be displayed. If the name is not found on either list Santa should be prompted to choose whether that person is naughty or nice. The name should then be added to the appropriate list (up to a maximum of 20 names for each list).

Main Algorithm: 1. 2. 3. 4. 5. 6. 7. 8. 9.

set up naughty list set up nice list get name of person from user search naughty list for person search nice list for person IF person not found THEN get naughty or nice selection from user add name of person to appropriate list ENDIF

Tasks: •

Using Microsoft Word, refine the above algorithm for steps 4, 5 and 8. Hint: Use a CASE statement for step 8.



Design a suitable HCI.



Print out your design. Let your teacher check this before proceeding further.



Using the earlier tasks to help you, implement your design using Visual Basic.



If you have time, make your HCI colourful and attractive.



Save program as Task_B3.



Print out the source code and a screenshot of your completed program. Let your teacher see your program working!



Using Microsoft Word, create suitable test data (normal, extreme and exceptional) for your program.



Print out the test data tables including the results of your tests.



Hand this task in to your teacher as evidence.

ISK 2006

4

Finding Max. / Min.

L04

Task C1: High Score Finding Maximum Value (including Input Validation) Write a program which displays a pre-defined list of scores and asks the user to input a new score. The program should then add the new score to the list and display a message showing the highest score in the list. It is assumed that the score entered by the user can be between 0 and 9999 and that the number of items in the list is always 5 including the new score.

Main Algorithm:

Visual Basic Code:

1. set up score list 2. get new score from user and add to list 3. find highest score in list

call set_up_score_list(score) call get_new_score_from_user(score) call find_highest_score(score)

Refinements: 2.1. get score from user 2.2. WHILE score is less than 0 or greater than 9999 2.3. prompt user to try again 2.4. get score from user 2.5. ENDWHILE 2.6. add score to score list

user_score = InputBox(“Enter score (0-9999)”) WHILE user_score < 0 or user_score > 9999 MsgBox(“Invalid score. Try again.”) user_score = InputBox(“Enter score (09999)”) WEND score(no_scores-1) = user_score

3.1.set largest so far to the value of the first item in the list 3.2.FOR each of the remaining items in the list in turn 3.3. IF item value > largest so far THEN 3.4. set largest so far = item value 3.5. ENDIF 3.6.NEXT item in list 3.7.report largest so far as maximum of list

high_score = score(0)

ISK 2006

FOR counter = 1 to (no_scores-1) IF score(counter) > high_score THEN high_score = score(counter) ENDIF NEXT counter picOutput.Print “Highest score is: “ & high_score

1

Finding Max. / Min.

L04

Tasks: •

Create the following form, ensuring your name and the current date are displayed in the label lblCredits: Picture Box picOutput Command Button cmdStart

Label lblCredits



Enter the Visual Basic main algorithm code given on the previous page within the cmdStart_Click subroutine.



Create each subroutine named in the above code using the refinements to help you. Use ByVal or ByRef accordingly to pass the score array into each subroutine.

Note: You should try to create the set_up_score_list in the most efficient way possible and display the scores in picOutput using the .Print command. e.g. picOutput.Print “Hello” •

Add the following Visual Basic code above the cmdStart_Click subroutine to define the variables used in the program.

Option Explicit ‘This means all the variables must be defined before use Const no_scores = 5 Dim score(0 To no_scores - 1) As Integer Dim counter As Integer Dim user_score As Integer Dim high_score As Integer •

Add a line of Visual Basic code to display the score input by the user in the picOutput Picture box before the highest score is displayed.



Save program as Task_C1.



Using Microsoft Word, create suitable test data (normal, extreme and exceptional) for your program. Complete the table and save as Test_C1.doc

ISK 2006

2

Finding Max. / Min.

L04

Task C2: Fastest Time Finding Minimum Value (including Input Validation) Write a program which displays a pre-defined list of 100m sprint times and asks the user to input a new time. The program should then add the new time to the list and display a message showing the fastest time. It is assumed that the time entered by the user can be between 0.00 and 59.99 and that the number of runners is always 5 including the user’s time. ** NOTE THE SIMILARITY TO TASK C1! **

Main Algorithm:

Visual Basic Code:

1. set up sprint times list 2. get new time from user and add to list 3. find fastest time in list

call set_up_sprint_times_list(sprint) call get_new_time_from_user(sprint) call find_fastest_time(sprint)

Refinements: 2.1. get time from user 2.2. WHILE time is less than 0.00 or greater than 59.99 2.3. prompt user to try again 2.4. get time from user 2.5. ENDWHILE 2.6. add time to sprint time list 3.1.set smallest so far to the value of the first item in the list 3.2.FOR each of the remaining items in the list in turn 3.3. IF item value < smallest so far THEN 3.4. set smallest so far = item value 3.5. ENDIF 3.6.NEXT item in list 3.7.report smallest so far as minimum of list

ISK 2006

user_time = InputBox(“Enter 100m sprint time (059.99)”) WHILE user_time < 0.00 or user_time > 59.99 MsgBox(“Invalid time. Try again.”) user_time = InputBox(“Enter 100m sprint time (0-59.99)”) WEND sprint(no_runners-1) = user_time fastest_time = sprint(0) FOR counter = 1 to (no_runners-1) IF sprint(counter) < fastest_time THEN fastest_time = sprint(counter) ENDIF NEXT counter picOutput.Print “Fastest time is: “ & fastest_time

3

Finding Max. / Min.

L04

Tasks: •

Open Task_C1 and save a copy as Task_C2. Make sure you save the FORM as Task_C2 as well!



Edit the source code to match the algorithm above. Note you will need to change the form caption, subroutine names, variable names AND TYPES. Remember: Variable Type String Integer Boolean Single

Description Contains text e.g. Name Whole numbers e.g. 42 True / False Real numbers e.g. 3.14



Add a line of Visual Basic code to display the sprint time input by the user in the picOutput Picture box before the fastest time is displayed.



Add comments to your source code including program title (Task C2…), name and date.



Save completed program as Task_C2.



Using Microsoft Word, create suitable test data (normal, extreme and exceptional) for your program. Complete the table and save as Test_C2.doc



Print out a copy of your source code AND your test data and hand in to your teacher for marking.

ISK 2006

4

Finding Max. / Min.

L04

Task C3: High Score with Player Name Finding position of Maximum Value (with Input Validation) Write a program which displays a pre-defined list of scores and associated player names and asks the user to input (1) their name and (2) a new score. The program should then add the user name and new score to the appropriate lists and display a message showing the highest score in the list and the associated player name. It is assumed that the score entered by the user can be between 0 and 9999 and that the number of items in both lists are always 5 including the new player name and score.

Main Algorithm (with data flow): 1. 2. 3. 4.

set up score list and player name list get user details find position of highest score display high score with player name

In score(), player() score(), player() score(), index score(), player(), index

Out score(), player() score(), player() index

Tasks: •

Using Microsoft Word, refine the above algorithm for ALL steps. Hint: Use your Standard Algorithms booklet to help with step 3.



Design a suitable HCI.



Print out your design. Let your teacher check this before proceeding further.



Using the earlier tasks to help you, implement your design using Visual Basic.



If you have time, make your HCI colourful and attractive.



Save program as Task_C3.



Print out the source code and a screenshot of your completed program. Let your teacher see your program working!



Using Microsoft Word, create suitable test data (normal, extreme and exceptional) for your program.



Print out the test data tables including the results of your tests.



Hand this task in to your teacher as evidence.

ISK 2006

5

Counting Occurrences

L04

Task D1: Number of Passes Counting occurrences Write a program which takes a pre-defined list of Pass / Fail grades and counts the number of “Pass” grades in the list. The program should display the result on the screen. It is assumed that the number of grades in the list is 10.

Main Algorithm:

Visual Basic Code:

1. set up grades 2. count occurrence of “pass” in list 3. display result

Call set_up_grades(grade()) Call count_passes(grade(), no_passes) Call display_result(no_passes)

Refinements: 2.1. set no. of passes counter to zero 2.2. loop through the items to be counted 2.3. IF the grade is a pass THEN 2.4. add 1 to counter 2.5. ENDIF 2.6. end loop

no_passes = 0 FOR counter = 0 to 9 IF grade(counter) = “Pass” THEN no_passes = no_passes + 1 ENDIF NEXT counter

Tasks: •

Create the following form, ensuring your name and the current date are displayed in the label lblCredits: List Box lstOutput Command Button cmdStart Label lblCredits



Enter the Visual Basic main algorithm code given on the previous page within the cmdStart_Click subroutine.



Create each subroutine named in the above code using the refinements to help you. Use ByVal or ByRef accordingly.



Add Visual Basic code above the cmdStart_Click subroutine to define the variables used in the program. Remember “Option Explicit”.



Save program as Task_D1.

ISK 2006

1

Counting Occurrences

L04

Task D2: Dutch Auction Linear search with count of matches (with Input Validation) A Dutch Auction is the sale of a item where people bid the lowest amount possible – hoping that their bid is unique. The person who bids the lowest unique amount will be declared the winner of the auction and can purchase the item for the amount they bid. For example: If there were 4 bids entered into the auction as follows: 1. 1p 2. 1p 3. 7p 4. 4p The lowest unique bid is 4p, therefore bidder 4 would be declared the winner. Write a program which takes a pre-defined list of bids and allows the user to add other bids by clicking on a button. When the user wants to display the lowest unique bid (by clicking on another button) the program should, for each bid amount, count the number of occurences in the list. If the bid is unique (i.e. the number of occurences is 1) then display a message telling the user which unique bid is the lowest. It is assumed that the bid amount is in pence between 1 and 9999 and the maximum amount of bids is 20.

Tasks: •

Copy Task_D2 from RMSharedDocuments -> Computing -> Higher Computing -> Software Development -> Standard Algorithms to your own programming folder.



Load and run the program to ensure that it works correctly (check against the problem statement above). At this point you should see a “Work in progress” message when you click on the “Find Lowest Unique Bid” button.



Using Microsoft Word, write out the stepwise refinements pseudocode for this program. The main algorithm is given below to help you. Hint: this time you convert Visual Basic code into a sub-English description. Main Algorithm: 1. set up 10 random bids 2. add bid to list 3. find lowest unique bid



Write out the stepwise refinements pseudocode for the subroutine find_and_count using the linear search with count of matches example in the theory booklet (p.4)



Implement your design for the subroutine find_and_count.



Uncomment the code in subroutine cmdFindUnique_Click() and remove the “Work in progress…” message.

ISK 2006

2

Counting Occurrences

L04



Customise your form in any way you choose (without affecting the functionality of the program).



Save completed program as Task_D2b.



Using Microsoft Word, create suitable test data (normal, extreme and exceptional) for your program. Complete the table and save as Test_D2.doc



Print out a copy of your pseudocode, source code (with suitable internal commentary), a screenshot AND your test data



Hand print outs in to your teacher for marking.

Checklist Have you completed the following programs? Completed?

Task

A1

Test Scores



A2

Talent Show



A3

School Houses



B1

Colours



B2

Pupil List



B3

21st Century Santa Claus



C1

High Score



C2

Fastest Time



C3

High Score with Player Name



D1

No. of Passes



D2

Dutch Auction



ISK 2006

Evidence seen by teacher

 Design  Source code  Screenshot  Source code  Design  Source code  Screenshot  Test data  Source code  Test data  Design  Source code  Screenshot  Test data  Design  Source code  Screenshot  Test data 3

Suggest Documents