1D arrays, 2D arrays, records, and how data structures appear in programs. Jun 2022 Q5(a)(i)/(ii) tested arrays directly.
Indexing typically starts at 0 in most languages. (Jun 2022 Q5(a)(i): "Array" or "List" both accepted. Q5(a)(ii): colours[1] = "Yellow".)
| Term | Definition |
|---|---|
| 1D array | |
| 2D array |
| Point | Detail |
|---|---|
| Record | |
| Arrays vs records | |
| Array of records | |
| Files |
| Data type | Example |
|---|---|
| Integer | |
| Real/Float | |
| Boolean | |
| String | |
| Character |
Choosing the wrong data type causes bugs — e.g. storing "42" as a string means you cannot do arithmetic on it.
| Point | Detail |
|---|---|
| Need for data structures | |
| String | |
| 2D array | |
| Example | |
| Iteration |
nums = [4, 8, 15]
total = 0
for n in nums:
total = total + n
print(total) # 27
grid = [[1, 2, 3], [4, 5, 6]]
for row in grid:
for item in row:
print(item)What type of data structure stores a collection of items of the same data type, accessed by index?
An array is defined as: colours = ["Red", "Green", "Blue", "Yellow", "Purple"]
What is the value of colours[1]? And what index does "Purple" have? (2 marks)
Explain the difference between an array and a record as data structures. (2 marks)
A program needs to store whether a light switch is on or off. Which data type is most appropriate?
grid = [[1, 2, 3], [4, 5, 6]] What is the value of grid[1][2]?
The list nums holds [4, 8, 15]. Write Python that adds up every item in nums and prints the total. (3 marks)