Date | November 2018 | Marks available | 6 | Reference code | 18N.1.HL.TZ0.12 |
Level | HL | Paper | 1 | Time zone | no time zone |
Command term | Construct | Question number | 12 | Adapted from | N/A |
Question
The following method, swap(A,B)
is written to exchange the values of variables A and B.
swap(A,B)
TEMP = A
B = TEMP
A = B
end swap
Method swapRows(MAT,K,L)
swaps the elements of two rows (row K
and row L
) in the two‑dimensional array MAT
.
For example,
A game consists of four rounds. In each round a player can score up to 100 points.
The data about the game is sorted in alphabetical order of names and stored in the memory as follows
WherePLAYERS
is a one-dimensional array holding names of players (currently sorted in alphabetical order).ROUNDS
is a two-dimensional array holding players’ scores.TOTALS
is a one-dimensional array holding total scores.
For example,PLAYER[1]
is Boris. The total number of points he scored is 180 and this can be found in TOTALS[1]
.
Boris scored 40 points in the first round which can be found in ROUNDS[1][0]
.
The value stored in ROUNDS[1][2]
is 50 which means that Boris scored 50 points in the third round.
State Paul’s total score.
State where Hugh’s score in the fourth round can be found.
All the data stored in memory should be sorted in ascending order of total score using selection sort.
For example, after sorting, the data given opposite will be held in memory as follows
Construct an algorithm that will sort all the data in ascending order.
In your solution you should call methods swap()
and swapRows()
given in part (a) and part (b).
Markscheme
Award [1 max].
105;
Award [1 max].
ROUNDS[2][3]
;
Award [6 max].
Award [1] for the correct outer loop (loops through the array TOTALS
).
Award [2 max] for correct determination of MINIM
/ the position of smallest element in part of array TOTALS
(range should be from J + 1 to 5), [1] for minor error.
Award [1] for correct condition / to check whether swaps are needed or not.
Award [1] for correct swap of elements in array TOTALS
.
Award [1] for correct swap of elements in array PLAYERS
.
Award [1] for correct swap of rows in two-dimensional array ROUNDS
.
Example answer:
loop for J from 0 to 5
MINIM = J
loop for I from J + 1 to 5
if TOTALS[I] < TOTALS[MINIM]:
MINIM = I
end if
end loop
if MINIM != J
swap(TOTALS[MINIM],TOTALS[J])
swap(PLAYERS[MINIM], PLAYERS [J])
swapRows(ROUNDS, MINIM, J)
end if
end loop