Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Worksheet 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Worksheet 3 Errors and testing

Unit 8 Logic and languages

Name:...................................................................................................... Class: ......................

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:

 Motorcycle £1.00 (Vehicle type 1)


 Car £2.00 (Vehicle type 2)
 Goods vehicle £2.50 (Vehicle type 3)
 HGV and coaches £5.00 (Vehicle type 4)
After 10pm and up to but not including 6am, there is no charge.

Amanda writes the following pseudocode:

if time <= 0600 OR time >= 2200 then


charge = 0
else
switch vehicle:
case 1:
charge = 1.00
case 2:
charge = 2.00
case 3:
charge = 2.50
case 4:
charge = 5.00
default:
charge = 0.00
endswitch
endif
print(charge)

4
Worksheet 3 Errors and testing
Unit 8 Logic and languages

Complete the following test plan.

Data Expected
Test Data Actual
(Vehicle Reason for test result for
number (Time) result
type) Charge

1 1 1245 Valid time 1.00 1.00

2 2 0600 Invalid Time 0.00 0.00

3 3 12:45 Valid Time 2.50 2.50

4 4 4000 Invalid Time 0.00 0.00

5 4 1500 Valid Time 5.00 5.00

(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

itemFound searchFailed first last midpoint names[midpoint] OUTPUT

False False 0 12 6

(b) The user searches for the name Erik. Complete the trace table below

itemFound searchFailed first last midpoint A[midpoint] OUTPUT

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)

However, when the program is run, it produces the following output.


'Alan', 'Alan', 'Alan', 'Alan', 'Alan', 'Mark'

7
Worksheet 3 Errors and testing
Unit 8 Logic and languages

(a) What sort of error is this?

(b) Describe how you might set about finding the error in the program.

(c) Which line is incorrect? What should it be?

You might also like