Worksheet 3
Worksheet 3
Worksheet 3
Task 1
Edward has drawn a flowchart to show the basic steps in a program for a game. A player
continues to roll two die, adding the numbers rolled to his score. The player can stop at any
time, but if he rolls a double before stopping his score goes back to zero.
(a) Use the flowchart to write a program in the language of your choice, from this algorithm.
1
Worksheet 3 Errors and testing
Unit 8 Logic and languages
2
Worksheet 3 Errors and testing
Unit 8 Logic and languages
(b) Write down up to three examples of syntax errors that you made in your program as you
developed it.
- colon misplacement
- missing brackets
-capitalised function
(b) Correct these errors and make sure your program works correctly.
(c) Insert three deliberate syntax errors in your program. Write them down below.
colon misplacement
- missing brackets
-capitalised function
(d) Now insert a logic error in the program. Test it yourself to see the effect. Write the error and
the expected result below. Swap with a partner and ask them to find and correct the error.
Score= die1+die2
(e) Swap with a partner and see if you can each find the other’s syntax errors without compiling
the program. Write down the syntax errors that you found.
None
(f) Write down the logic error that your partner added to their program.
Dice1=random(1,7)
3
Worksheet 3 Errors and testing
Unit 8 Logic and languages
Task 2
The pseudocode program shown below is intended to model the calculation of the toll charge
for crossing a bridge on a motorway. Between 6am (0600hrs) and 10pm (2200 hrs) inclusive,
the charges are as follows:
4
Worksheet 3 Errors and testing
Unit 8 Logic and languages
Data Expected
Test Data Actual
(Vehicle Reason for test result for
number (Time) result
type) Charge
(a) Are your tests sufficient to thoroughly test the program? What assumptions have
been made?
There would not be a completely different vehicle eg. A lorry which would get free parking
(b) Is the logic of the program correct? If not, what should be changed?
It is correct
Task 3
The following algorithm performs a binary search to find an item in a sorted array A of length n.
If the item is found, its position in the array is displayed. If the item is not in the list, “Item not
found” is displayed. Assume that the first element of the array is A[0].
itemFound = False
searchFailed = False
Array names[13]
names = ["Anna", "Bill", "David", "Faisal", "Jasmine", "Jumal", "Ken",
"Michela", "Pavel", "Rosa", "Stepan", "Tom", "Zac"]
first = 0
last = names.length – 1
do
midpoint = int((first + last)/2)
if names[midpoint] == itemSought then
itemFound = True
else
if first > last then
searchFailed = True
else
if names[midpoint] < itemSought then
first = midpoint + 1
5
Worksheet 3 Errors and testing
Unit 8 Logic and languages
else
last = midpoint - 1
endif
endif
endif
until itemFound OR searchFailed
if itemFound then
print("Item is at position " + str(midpoint))
else
print("Item is not in the array")
endif
6
Worksheet 3 Errors and testing
Unit 8 Logic and languages
(a) The user searches for the name Tom. Complete the trace table below
False False 0 12 6
(b) The user searches for the name Erik. Complete the trace table below
Task 4
The following pseudocode is supposed to sort an array of names into ascending alphabetical
order.
1 userName = ["Carl","Tamsin","Eric","Zoe","Alan","Mark"]
2 numItems = 6
3 while numItems > 1
4 for count = 0 to 4
5 if userName [count] > userName[count+1] then
6 temp = userName[count+1]
7 userName[count] = userName[count+1]
8 userName[count+1] = temp
9 endif
10 next count
11 numItems = numItems – 1
12 endwhile
13 print (userName)
7
Worksheet 3 Errors and testing
Unit 8 Logic and languages
(b) Describe how you might set about finding the error in the program.