Store and process multiple values efficiently — in OCR ERL and Python
| Index | [0] | [1] | [2] | [3] | [4] |
|---|---|---|---|---|---|
| scores | 85 | 72 | 91 | 65 | 78 |
← [...]0 TO 4 for a 5-element array. In Python, range(5) gives 0,1,2,3,4 — or use range(len(array)) to work with any size.array[row][col]. Both row and column indices start at 0.| col [0] | col [1] | col [2] | |
|---|---|---|---|
| row [0] | 1 | 2 | 3 |
| row [1] | 4 | 5 | 6 |
| row [2] | 7 | 8 | 9 |
board[1][1] → the highlighted 5arrayName[row][col]| board[r][c] | Position | Value |
|---|---|---|
| [0][0] | Top-left | 1 |
| [0][2] | Top-right | 3 |
| [2][0] | Bottom-left | 7 |
| [2][2] | Bottom-right | 9 |
board[row][col]. The only difference is ← vs = for assignment.| row | col | grid[row][col] | output |
|---|---|---|---|
| 0 | 0 | grid[0][0] | 1 |
| 0 | 1 | grid[0][1] | 2 |
| 0 | 2 | grid[0][2] | 3 |
| col[0] Name | col[1] Year | col[2] Score | |
|---|---|---|---|
| row[0] | Ali | 10 | 85 |
| row[1] | Beth | 11 | 92 |
| row[2] | Carl | 10 | 78 |
temps ← [5, 12, 8, 20, 3]temps[3].marks with 4 elements, all initialised to 0. Then write one statement to store 75 in the third element.marks[3] — that is the fourth element.
data is declared as:data ← [["Ali",85],["Beth",92],["Carl",78]]data[2][0]. (1 mark)i because it changes each iteration.
data[i][0] instead of data[i][1] — that prints the name, not the score1 TO 3 instead of 0 TO 2 — off by one, misses row 0| col [0] Name | col [1] Score | |
|---|---|---|
| row [0] | Ali | 85 |
| row [1] | Beth | 92 |
| row [2] | Carl | 78 |
data[2][0] → "Carl" (row 2, col 0 highlighted above)scores[1] to access the first element — arrays are 0-indexed. The first element is always index 0scores[0] · Third element = scores[2] · Always subtract 1 from the positiongrid[col][row] when the question means row first — the order matters and produces the wrong element[row][col] — row is the outer index, column is the inner indexFOR i = 1 TO 5 for a 5-element array — this accesses indices 1–5, but valid indices are 0–4. Index 5 does not existFOR i = 0 TO 4 — always start at 0, end at length−1name ← [v0, v1, v2...]; access with name[i]; update with name[i] ← value; iterate with FOR i = 0 TO length−1name[row][col]; iterate with nested FOR loops — outer for rows, inner for columnsGet the full resource pack at CSZone.co.uk