Click to see the rest of the programming section :
In programming, the use of records for storing data allows us to use arrays more efficiently. When using an array to store data, we are restricted to a single data type, so in order to store multiple data types about the same thing we have two options: parallel arrays, or records.
Parallel Arrays use multiple lists (1D arrays) where related data is stored in the same index, or position in each list.
This allows us to create a different array for each type of data.
In the example below, we are storing the names of different players in a game and their scores. TO do this, we need to store the name as a string, and the score as an integer. Using parallel arrays, in pseudocode this would look like (remember for AQA, substitute the = for ß):
OUTPUT names + “scored “ + scores
This algorithm has been implemented in the python below. Try changing the code so that a loop is used to output each of the names along with the score in a concatenated string.
Parallel arrays certainly work, but can become very complex when many items of data are saved, or the data objects have many fields. In this case, it becomes more efficient (and easier for the programmer) to use a record. Using the same data, we could create the following pseudocode for a record:
Player1 = Player(“Tina”,3)
Player2 = Player(“Gene”,8)
Player3 = Player(“Louise”,12)
Try adjusting the Python code below so that each player and their score is output in order. To run the code in the browser, press Ctrl + Enter.
In Python, the data structure that we have used to create a record is referred to as a dictionary. This makes the syntax slightly different to other programming languages.
Once a record has been created, it can then be added to a list, making an array of records. In effect, this creates a data structure that can be used like a 2D array but with multiple data types, and named columns instead of an index.
Try adjusting the Python code below to add some additional records to the array and then output each one in turn. To run the code in the browser, press Ctrl + Enter.
In the same way as before, whilst other languages use similar syntax to pseudocode to create and output the elements of a record, Python syntax is a little different. Make sure that you have created revision notes for both pseudocode and your chosen programming language.
Found this page helpful? Please consider sharing!