National 5 used two data structures: variables and arrays. There are new data structures you need to know about for Higher: parallel arrays, record structures, and arrays of records.
Parallel arrays are two or more arrays of the same size, such that:
the 1st element of array A is related to the 1st element of array B and the 1st element of array C
the 3rd element of array A is related to the 3rd element of array B and the 3rd element of array C
the 10th element of array A is related to the 10th element of array B and the 10th element of array C
etc.
Each array can only store data of the same data type
In code, there is no special syntax or different way to write these. We just declare three arrays, whereas most of the programs you’ve used would have previously only had one.
pupilName = ["Arthur", "Jessica", "Alex", "Sam", "Rebecca"]
prelimMark = [110, 90, 32, 67, 95]
courseMark = [45, 30, 8, 33, 40]
for counter in range(0,4):
print(pupilName[counter], "achieved a mark of", prelimMark[counter], "in their prelim and", courseMark[counter], "in their coursework.")
for counter in range(0,4):
print(pupilName[counter])
print(prelimMark[counter])
print(courseMark[counter])
In Software Design and Development, a record is a programmer-defined data type.
A record can be used in programming to store multiple variables and different data types, similar to a database record that contains multiple fields and different data types.
from dataclasses import dataclass
@dataclass
class pupil:
name: str
prelimMark: int
courseMark: int
print(pupil1.name)
print(pupil1.prelimMark)
print(pupil1.courseMark)
pupil1 = pupil("Arthur", 110, 45)
pupil1.prelimMark = 120
An array of records can be used to store multiple records, each containing different variables and data types. This is similar to a database table of records.
The first step is still to define your record structure.
from dataclasses import dataclass
# Define record structure
@dataclass
class Game:
title: str = "Game Title"
platform: str = "Xbox One"
rating: float = 0.0
multiplayer: bool = False
games = [Game] * 3
games[0].title= "Mass Effect 3"
games[0].platform= "Xbox One"
games[0].rating= 5.0
games[0].multiplayer = True
for counter in range(0,3):
games[counter].title= input("\nEnter the title: ")
games[counter].platform= input("Enter the platform: ")
games[counter].rating= float(input("Enter the rating: "))
games[counter].multiplayer= input("Is the game multiplayer (true/false): ")
for counter in range(0, 3):
print(games[counter].title)
print(games[counter].platform)
print(games[counter].rating)
print(games[counter].multiplayer)