Medium3 marksStructured
Fundamentals of algorithmsGeneralalgorithms2D arrayspseudo-code

AQA GCSE · Question 14 · Fundamentals of algorithms

Figure 16 shows an incomplete algorithm, represented using pseudo-code, designed to output the highest or lowest results of a vote. The programmer has used a two-dimensional array called results to store the genre and the number of votes for each genre. Parts of the algorithm are missing and have been replaced with the labels L1 to L3.

Figure 16
SUBROUTINE showResults(method, numberOfGenres)
results ← [['Pop', 'Post-Punk', 'Techno', 'Metal', 'Dance'],
['7', '19', '14', '1', '9']]
pos ← 0
high ← -1
IF method = 'HIGHEST' THEN
FOR i ← 0 TO numberOfGenres - 1
Votes ← STRING_TO_INT(results[L1][i])
IF votes > high THEN
high ← votes
pos ← L2
ENDIF
ENDFOR
ELSE
OUTPUT 'not yet working'
ENDIF
IF high ≠ -1 THEN
OUTPUT results[0][pos], ' with ', results[1][pos]
ENDIF
ENDSUBROUTINE

OUTPUT 'Show the genre with the HIGHEST or LOWEST number of votes? '
method ← USERINPUT
showResults(L3, 5)
State what should be written in place of the labels L1 to L3 in the algorithm.

How to approach this question

1. **Analyse L1:** The line is `Votes ← STRING_TO_INT(results[L1][i])`. The `results` array is 2D. `results[0]` contains the genres (strings), and `results[1]` contains the votes (as strings). The code needs to access the votes. Therefore, the first index must be 1. So, L1 is `1`. 2. **Analyse L2:** The line is `pos ← L2`. The variable `pos` is used to store the *position* (index) of the genre with the highest number of votes found so far. The `FOR` loop uses the variable `i` to iterate through the indices of the genres. When a new highest vote is found at index `i`, `pos` should be updated to `i`. So, L2 is `i`. 3. **Analyse L3:** The line is `showResults(L3, 5)`. This is the call to the subroutine. The subroutine is defined as `showResults(method, numberOfGenres)`. The first parameter it expects is the method (e.g., "HIGHEST"). The program gets this value from the user and stores it in the variable `method`. Therefore, the `method` variable should be passed as the first argument. So, L3 is `method`.

Full Answer

L1: `1` L2: `i` L3: `method`
This question requires understanding how 2D arrays are indexed and how variables are used within loops and passed to subroutines. - **L1:** `results` is a 2D array (or list of lists). `results[0]` is the list of genres. `results[1]` is the list of vote counts. The line `Votes ← STRING_TO_INT(results[L1][i])` is converting a vote count from a string to an integer. Therefore, it must be accessing the second sub-array, which has an index of 1. - **L2:** The algorithm is a standard "find maximum" routine. It iterates through the list, keeping track of the highest value seen so far (`high`) and the index where it was found (`pos`). The loop variable `i` represents the current index being checked. If `votes` at the current index `i` is greater than the current `high`, we have a new maximum. We must then update `pos` to store this new index, so `pos ← i`. - **L3:** The program asks the user for input and stores it in the `method` variable. This variable is then used to call the `showResults` subroutine. The subroutine definition shows that it accepts `method` as its first parameter. Therefore, the `method` variable must be passed in the call: `showResults(method, 5)`.

Common mistakes

✗ For L1, using 0, which would try to convert a genre name like "Pop" to an integer.\n✗ For L2, using a literal number or another variable like `pos` instead of the loop counter `i`.\n✗ For L3, using a string literal like `"HIGHEST"` instead of the variable `method` that holds the user's input.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam