ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
8 marks · Testing and debugging
The contents of the drinkTypes and numOrders arrays are shown below. drinkTypes Tea Coffee Tea Water Coffee Water Tea numOrders 1 3 2 6 1 4 2 Using the data above, complete the trace table below by identifying the missing values in the highlighted boxes. Line number index totals currentDrink currentDrinkTotal 5 0 6 [0,0,0] 7 8 5 9 [5,0,0] 5 1 6 7 8 9 Identify the line number causing the error and describe the change required to make the code fit for purpose. Line number Change Describe how a watchpoint could be used to test code. Staff at an event use an app to record the number of times they serve tea, coffee and water. The details of the drinks served are collected and stored in parallel arrays named drinkTypes and numOrders as shown below. drinkTypes Tea Coffee Tea Water Coffee Water Tea numOrders 1 3 2 6 1 4 2 The app will count and store the number of times each type of drink has been served in an array called totals. For example, using the data above there are 5 teas, 4 coffees and 10 waters. The expected values in the totals array would be [5,4,10]. The code below is used to implement this. When the code was tested an error was found. Line 1 FUNCTION checkItems(ARRAY OF STRING drinkTypes, ARRAY OF INTEGER numOrders) RETURNS ARRAY OF INTEGER Line 2 DECLARE currentDrink INITIALLY "" Line 3 DECLARE drinks INITIALLY AS ["Tea","Coffee","Water"] Line 4 DECLARE currentDrinkTotal INITIALLY 0 Line 5 FOR index FROM 0 TO length(drinks)-1 DO Line 6 DECLARE totals INITIALLY [0] * 3 Line 7 SET currentDrink TO drinks[index] Line 8 SET currentDrinkTotal TO countItems(drinkTypes,numOrders,currentDrink) Line 9 SET totals[index] TO currentDrinkTotal Line 10 END FOR Line 11 RETURN totals Line 12 END FUNCTION ... … Line 20 FUNCTION countItems (ARRAY OF STRING drinkTypes, ARRAY OF INTEGER numOrders, STRING currentDrink) RETURNS INTEGER Line 21 DECLARE total INITIALLY 0 Line 22 FOR counter FROM 0 TO length(drinkTypes)-1 DO Line 23 IF drinkTypes[counter] = currentDrink THEN Line 24 SET total TO total + numOrders[counter] Line 25 END IF Line 26 END FOR Line 27 RETURN total Line 28 END FUNCTION
• Line 6 totals = [0,0,0] • both updates of currentDrink • Line 8 currentDrinkTotal = 4 • Line 9 totals = [0,4,0] Line Number index totals currentDrink currentDrinkTotal 5 0 6 [0,0,0] 7 Tea 8 5 9 [5,0,0] 5 1 6 [0,0,0] 7 Coffee 8 4 9 [0,4,0] • Line 6 • move the declaration outside of the loop/swap lines 5 and 6 • stops execution when a variable changes/meets condition • allowing variable values to be compared to expected values/allows variable to be inspected