Date | November 2019 | Marks available | 4 | Reference code | 19N.1.SL.TZ0.10 |
Level | SL | Paper | 1 | Time zone | no time zone |
Command term | Describe | Question number | 10 | Adapted from | N/A |
Question
NUMBERS
is a collection that holds only positive integers.
A three-digit number has three digits: a hundreds digit, a tens digit and a units digit.
For example, for 406, its hundreds digit is 4, its tens digit is 0 and its units digit is 6.
An algorithm is needed to copy each three-digit number from the collection NUMBERS
, where the hundreds digit is smaller than its tens digit and its tens digit is smaller than its units digit, into a one-dimensinal array named THREE
. If there are no such numbers in the collection then an appropriate message should be displayed.
For example:
If NUMBERS
= {9, 3456, 12, 237, 45679, 368, 296}
then the contents of the array, THREE
, is:
If NUMBERS
= {1234, 56, 90, 324, 876}
then the array THREE
is empty and a message such as “No such numbers”, should be outputted.
Consider the following algorithm.
N = 372
X = N DIV 100
Y = X + 10 * (N MOD 100 DIV 10)
Z = Y + (N MOD 10) * 100
Determine the values of variables X, Y, andZ
after execution of this algorithm. Show your working.
Construct this algorithm. You may assume that the array THREE
is initialized with a sufficient number of elements.
Describe how a selection sort algorithm could be used to sort the array THREE
in ascending order.
Markscheme
Award [3 max]
X = 372 div 100 = 3;
Y = 3 + 10 * (372 mod 100 div 10) = 3 + 10 * ((372 mod 100) div 10) = 3 + 10 * (72 div 10) = 3 + 10 * 7 = 73;
Z = 73 + (372 mod 10) * 100 = 73 + 2 * 100 = 273;
Award FT marks if working is shown.
Award 1 mark only for each correct value if no working shown, up to 3.
X = 3;
Y = 73;
Z = 273;
Allow FT marks.
Award [8 max]
Award [1] for reset/starting from the first item in the collection and for initialization and correct increasing of the array index (K)
Award [1] for the while loop through the collection
Award [1] for retrieving a number from the collection
Award [1] for if statement- checking whether the number retrieved is a three-digit number
Award [1] for correctly calculated digits in retreived number,
Award [1] for each
Award [1] for if statement which compares the three digits of the retrieved number
Award [1] for correctly placing ITEM into the array THREE
Award [1] for the correct output message
Example answer:
K=-1
NUMBERS.resetNext()
loop while NUMBERS.hasNext()
ITEM=NUMBERS.getNext()
if ITEM>99 and ITEM<1000 then
F=ITEM DIV 100
S=ITEM MOD 100 DIV 10 //or S= ITEM DIV 10 MOD 10
T=ITEM MOD 10
if F<S and S<T then
K=K+1
THREE[K]=ITEM
end if
end if
end loop
if K==-1 then
output('No such numbers')
endif
Award [4 max]
The selection sort algorithm starts by finding the minimum/smallest value in the array THREE (containing K elements);
and moving it to the beginning of the array THREE (element at the first position is THREE[0]) / exchanges it with the element in the first position;
the correct entry is in the first place in the array and the process is repeated on the remaining entries / this step is then repeated for the second lowest value, then the third, and so on;
once this has been repeated K-1 times;
the K-1 smallest entries are in the first K-1 places which leaves the largest element in the last place (the array is sorted in ascending order);
Examiners report
Candidates generally found this question difficult. They were required to determine the values of a number of variables using the formulae given. These formulae included program code for integer division and the remainder of integer division. Unfortunately, candidates often ignored this and simply gave answers as though regular division was used, therefore the answers given were incorrect. However, some candidates achieved full marks, and others achieved marks for partially correct responses.
A wide range of marks were awarded for this question, in which an algorithm was written. The algorithm involved copying data from a collection, processing it and then copying some of the data that meets a test into an array. Candidates achieved credit for the correct elements of their algorithms. Some common errors included candidates using an array for the initial data retrieval, rather than a collection, and not correctly using a loop, to ensure all the data elements had been read from the collection.
Some candidates achieved high marks for this question, as they were able to correctly describe how a selection sort could be used to sort the resulting array from part b in ascending order. A few candidates lost marks because they used the wrong type of sorting algorithm, for example, a bubble sort or an insertion sort. Another error seen involved candidates sorting the data into descending order.