An automated greenhouse irrigation system controls a row of 7 watering valves, indexed 0 to 6.
The subroutines in the table below are used to control the state of the valves, switching them between active and inactive.
| Subroutine | Explanation |
|---|---|
TOGGLE(n) | If the valve at index n is active it is set to inactive. If it is inactive it is set to active. |
COPY(n) | If the valve at index (n - 1) is active, the valve at index n is set to active. If the valve at index (n - 1) is inactive, the valve at index n is set to inactive. |
CLEAR_BETWEEN(m, n) | All valves strictly between index m and index n (but not including m and n) are set to inactive. |
If the starting array of valves is:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| active | active | active | active | active | active | active |
Write an algorithm, using exactly three subroutine calls, that means the final array of valves will be:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| inactive | inactive | inactive | inactive | inactive | inactive | inactive |
You must use each of the subroutines TOGGLE, COPY and CLEAR_BETWEEN exactly once in your answer. If you do not do this you may still be able to get some marks.
208 exam-style questions on AQA GCSE Computer Science Representing algorithms. Each one has a worked solution and a mark scheme showing where the marks go.