Chapter assessment
1. Use the following pseudo code with your own choice of values of X.
10 INPUT X 20 LET Y = X/3 30 LET R = Y – INT(Y) 40 IF R = 0 PRINT ACCEPT, GOTO A 50 PRINT REJECT 60 LABEL A 70 END
(i) Explain what is being achieved. [4]
(ii) If you replaced the line LET Y = X/3 by the line LET Y = X/5, how would this change the outcome of the above? [1]
2. The following algorithm is to be applied to the positive integers from 1 to 12.
Step 1: Cross out every even number. Step 2: Change the state of every multiple of 3 (including 3) – i.e. for every multiple of 3, if it is crossed out then remove the crossing out, and if it is not crossed out then cross it out. Step 3: Change the state of every multiple of 4 (including 4). Step 4: Change the state of every multiple of 5 (including 5). … … Step 11: Change the state of every multiple of 12 (including 12).
(iii) Numbers | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 2 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 3 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 4 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 5 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 6 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 7 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 9 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 10 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Step 11 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Apply the algorithm, using a row of the table below for each step. In each step the crossings-out should be the same as in the previous step except for those numbers whose state is changed. [4]
(iv) Say what the algorithm