Medium3 marksStructured
AQA GCSE · Question 12.4 · Fundamentals of algorithms
Figure 13 shows an algorithm, represented using pseudo-code, that should display currency names in reverse alphabetical order, starting with yen. There are errors in the logic of the algorithm.
Figure 13
1 SUBROUTINE diffCurrencies(currencies)
2 currencies ← ['baht', 'dollar', 'euro', 'koruna', 'lira', 'rand', 'rupee', 'yen']
3 RETURN currencies[x]
4 ENDSUBROUTINE
5
6 FOR i ← 8 TO 0 STEP 1
7 OUTPUT(diffCurrencies(i))
8 ENDFOR
Rewrite line 1 and line 6 from Figure 13 to make the algorithm work as intended.
Figure 13 shows an algorithm, represented using pseudo-code, that should display currency names in reverse alphabetical order, starting with yen. There are errors in the logic of the algorithm.
Figure 13
1 SUBROUTINE diffCurrencies(currencies)
2 currencies ← ['baht', 'dollar', 'euro', 'koruna', 'lira', 'rand', 'rupee', 'yen']
3 RETURN currencies[x]
4 ENDSUBROUTINE
5
6 FOR i ← 8 TO 0 STEP 1
7 OUTPUT(diffCurrencies(i))
8 ENDFOR
Rewrite line 1 and line 6 from Figure 13 to make the algorithm work as intended.
How to approach this question
1. **Analyse the Goal:** The aim is to print the list of currencies in reverse order. The given list is already sorted alphabetically.
2. **Identify Errors:**
* The subroutine structure is flawed. The main loop calls the subroutine, but the subroutine itself is broken (it uses an undefined variable `x` and re-initialises the list on every call). The simplest way to fix this is to remove the subroutine and just have a list and a loop.
* The `FOR` loop `FOR i ← 8 TO 0 STEP 1` is incorrect. The list has 8 items, so indices are 0 to 7. The loop should start at the last index (7). The `STEP 1` will not count down. To count down, it needs to be `STEP -1`.
3. **Rewrite Line 1:** Replace the broken subroutine definition with a simple array definition: `currencies ← ['baht', 'dollar', 'euro', 'koruna', 'lira', 'rand', 'rupee', 'yen']`.
4. **Rewrite Line 6:** Correct the loop to iterate from the last index (7) down to the first index (0) with a step of -1: `FOR i ← 7 TO 0 STEP -1`.
5. (Implicitly, line 7 would then need to be `OUTPUT currencies[i]`).
Full Answer
**Line 1:** `currencies ← ['baht', 'dollar', 'euro', 'koruna', 'lira', 'rand', 'rupee', 'yen']`
**Line 6:** `FOR i ← 7 TO 0 STEP -1`
The provided algorithm has several logical errors that prevent it from working.
1. **Scope and Structure:** The `currencies` list is defined inside the subroutine, but the loop trying to use it is outside. The subroutine itself is non-functional. The most direct way to fix the structure is to eliminate the subroutine and define the list in the main scope. So, line 1 becomes a simple list assignment.
2. **Loop Bounds:** The list has 8 items. With 0-based indexing, the valid indices are 0, 1, 2, 3, 4, 5, 6, 7. The loop `FOR i ← 8 TO 0` starts at an index (8) that is out of bounds. To iterate in reverse, it must start at the last valid index, which is 7.
3. **Loop Step:** The loop `STEP 1` instructs the loop to count up. To count down from 7 to 0, the step must be negative, i.e., `STEP -1`.
By making these two corrections, the algorithm will correctly define the list and then loop from index 7 down to 0, printing each currency in reverse alphabetical order.
Common mistakes
✗ Only fixing part of the `FOR` loop (e.g., changing the bounds but not the step).\n✗ Trying to fix the broken subroutine instead of replacing it with a simpler, working structure.\n✗ Off-by-one errors in the loop bounds (e.g., starting at 8 or ending at 1).
Practice the full AQA GCSE Computer Science Paper 1 Python
31 questions · hints · full answers · grading
More questions from this exam
Q01.1Figure 1 shows an algorithm, represented using pseudo-code, which assigns a different value to fo...EasyQ01.2The variable `x` is assigned a value using the statement:
`x ← LEN(state)`
Using Figure 1, what ...EasyQ01.3What is the result of concatenating the contents of the variables `city` and `landmark` in Figure 1?EasyQ01.5The subroutine `POSITION` finds the first position of a character in a string.
For example, `POSI...EasyQ02.1Figure 2 shows an algorithm that uses integer division which has been represented using pseudo-co...Easy
Expert