Slides badge

Data types and structures

Learning Intentions

  • Understand and use parallel arrays in a programming language

  • Understand and use records in a programming language

  • Implement an array of records

Success Criteria

  • Be able to explain and use parallel arrays in Python
  • Be able to explain and use records in Python
  • Be able to implement an array of records in Python

Data types

  • Data types are a feature of programming languages that allow the computer to store (in memory) and represent data.

  • For example, an integer takes 32 bits of memory to represent a number whereas a real number takes 64 bits (in most cases).

  • If you think about memory as a stream of data:
    111010010101011110010101010101110000000001111111

  • How does the computer know where one piece of data ends and another starts?

  • The answer is that it doesn't!

Data types

  • Name as many data types as you can from National 5.

String

Character

Boolean

Integer

Real/Floating Point

So how does it know what each bit means?

  • The above is the same binary string, but this time it is separated.
  • The computer knows that the first 32 bits are a 32-bit integer, so it simply reads the first 32 bits and knows this is the value of a variable. It also knows that in the next memory location there are 16 bits allocated to a 16-bit single precision floating point number.
  • So:
    • The computer knew how many bits are allocated to each value because it knew what the data type was.

111010010101011110010101010101110000000001111111

Variables

  • In a high-level language we use variables.
  • Variables make it easier for us to identify the storage location of data in memory since we give a variable a name (for example, in the C programming language you can write *6943 which will retrieve data found in memory location 6943, whereas in languages like Python we just write the name of the variable)
  • To the computer, variables are used to store the data type of the value and the memory location of the value.
  • A variable name is known as it's identifier.

Arrays

  • Sometimes it is necessary to store groups of data, e.g. a bunch of marks in one go. 
  • Because of the way computers have been designed with loops and repetitions, it becomes very easy to work with these groups.
  • Arrays are a set of data items of the same type, stored in one variable.
  • Each individual part of an array is called an element

Arrays

  • Arrays are defined using the code shown on line 1. This also called initialisation of an array. This is specifying that the array has four 'spaces' in it (0, 1, 2, 3).
  • The following lines are used to put values into the array.
name = [""] * 4

name[0] = "John"
name[1] = "Peter"
name[2] = "Hamish"
name[3] = "James"

Arrays

  • Accessing a value using the square brackets syntax, e.g. name[0], will access the first element of the array. The number in the square brackets is known as the element's index.
  • The square brackets syntax is known as a subscript.
  • If we try to set name[4] the program will crash. Why?
name = [""] * 4

name[0] = "John"
name[1] = "Peter"
name[2] = "Hamish"
name[3] = "James"

Arrays

  • When working with arrays, we can also very easily work with loops
  • The newly added lines 8 and 9 will iterate through the array, printing each name
name = [""] * 4

name[0] = "John"
name[1] = "Peter"
name[2] = "Hamish"
name[3] = "James"

for i in range(0, 4):
    print(name[i])

Arrays

  • Arrays with just one number as their subscript (e.g. name[""]) are called one-dimensional arrays.
  • Two-dimensional arrays have two numbers in their subscript, e.g. cells[0, 0] (e.g. a cell in a spreadsheet would have an x and a y)

For each of the following, calculate how many bits are required to store each array. Remember, the arrays are initialised with the value in subscript. Assume an integer is 32-bit, a floating point number is 64-bit and characters are 16 bit.

 

  1. ages[0] * 20
  2. names[""] * 10
  3. marks[0] * 10
  4. temperatures[0.0] * 12

 

Calculate the size of each array

640 bits

160 bits

320 bits

768 bits

Arrays in the SQA reference language

  • We can declare arrays using the SQA Reference Language (SQARL) as shown below:

 

 

 

  • And in Python, the same array could be written as:
forename = [""] * 4

DECLARE forename AS ARRAY OF STRING INITIALLY [] * 4

Parallel arrays

  • When two things run parallel to each other, they are running side by side.
  • Parallel arrays are useful for storing multiple pieces of data about several subjects in one go.
  • For example, if instead of just storing several marks, we also stored the user's first name and surname, we could use several arrays in parallel. 
    • Within these arrays, each individual person would have their own unique index in the array

Parallel arrays

forename = [""] * 3
surname = [""] * 3
mark = [0] * 3

forename[0] = "Nick"
forename[1] = "Peter"
forename[2] = "Calum"

surname[0] = "Thompson"
surname[1] = "Skeldon"
surname[2] = "Cormack"

mark[0] = 14
mark[1] = 19
mark[2] = 13

Declare three empty arrays

Set all of Nick Thompson's details to the first position in each array

Parallel arrays

And then display everything

...
for i in range(0, 3):
  print(forename[i], surname[i], "you got", mark[i], "out of 20.")

Parallel arrays

Parallel arrays in SQA Reference Language

  • We can declare the previous parallel arrays using the SQA Reference Language (SQARL) as shown below:

DECLARE forename AS ARRAY OF STRING INITIALLY [] * 3
DECLARE surname AS ARRAY OF STRING INITIALLY [] * 3
DECLARE mark AS ARRAY OF INTEGER INITIALLY [] * 3

Using Python IDLE, produce programs that meet the following requirements using parallel arrays:

 

  1. Write a program to store 4 questions and 4 correct answers (as integers). E.g. "True or False: Iron Man's armour is green". In the correct answers array it would contain False. Ask the user to answer each of the four questions.
  2. Write a program to store 4 users forenames, surnames, dates of birth and their favourite colour. Count up and display how many of them said that orange was their favourite colour.

Task

Record structure

  • A record structure is another data structure you need to know about for Higher Computing Science.
  • Parallel arrays do allow us to store data about a user such as:
    • Forename
    • Surname
    • Date of birth
  • However, the data is split across three arrays. What if the data about one person was stored in one place?

Record structure

  • Introducing the record structure!
  • A record structure is used to store data about one thing or object. 
  • The data stored about each thing or object in the record is the same for each user, only different in it's value.
  • For example, each record could store a forename, surname and date of birth

RECORD person IS {STRING forename, STRING surname, STRING date_of_birth}

Records in SQA Reference Language and Python

  • We can declare a record using the SQA Reference Language (SQARL) as shown below:

 

 

 

  • Python doesn't have a record type, but the closest structure is that of a dataclass in Python:
from dataclasses import dataclass

@dataclass
class person:
  forename : str = ""
  surname : str = ""
  date_of_birth : str = ""
  mark : int = 0

RECORD person IS {STRING forename, STRING surname, STRING date_of_birth, INTEGER test_mark}

Typing properties of a class

  • We can, at least for the sake of the SQA, add a data type to the properties of the record structure:
@dataclass
class person:
  forename : str = ""
  surname : str = ""
  date_of_birth : str = ""
  mark : int = 0

RECORD person IS {STRING forename, STRING surname, STRING date_of_birth, INTEGER test_mark}

Examples

@dataclass
class subject:
  subjectName : str = ""
  subjectLeader : str = ""
  numberOfPupils : int = 0
@dataclass
class car:
  manufactuer : str = ""
  yearOfRelease : int = 0
  modelName : str = ""

Instantiating records

  • Whenever we create a record structure like before, we can think of it as being like a template. 
  • We can then create any individual records for each thing or object from that template.
  • When we create one individual record based on a template, we call it a record instance.

Create a record instance

  • We can create an instance as shown below in the SQA Reference Language:

 

 

 

  • And in Python
JohnSmith = person()

JohnSmith.forename = "John"
JohnSmith.surname = "Smith"
JohnSmith.date_of_birth = "13/08/2001"
JohnSmith.mark = 75

DECLARE JohnSmith INITIALLY person ("John", "Smith", "13-08-2001", 75)

Parallel arrays in SQA Reference Language

Example

  • If we think about the DVLA, who hold information on all drivers, they will have a template for how driving licenses should look.
  • When someone passes a driving test, they create an instance of this template, and populate it with the person's details.

Comparing records to parallel arrays

  • Records hold many advantages of parallel arrays:
    • Removal of a record from an array deletes all information about that person or object in one go, parallel arrays require you to apply a removal to all arrays
    • Information is kept in one place and is tidier
  • However, parallel arrays require less code and are much less complex to implement

Create the following record structure using SQA Reference Language and then Python:

  1. A driving license record on the DVLA system containing forename, surname, driving license number, date of birth, full pass or not, years driving.
  2. A pupil record for exam results. It should have forename, surname, registration class, geography results, history results and modern studies results.
  3. A pet record for a vet. It should have the pet's names, the type of pet, whether the pet has been immunised or not, the age of the pet, the owner's surname and whether or not the owner has other pets

Task

Arrays of records

  • One area which comes up a lot in Higher Computing Science is using arrays of records.

DECLARE peopleArray AS ARRAY OF person INITIALLY [] * 2

 

SET peopleArray[0] TO {forename="John", surname="Smith", date_of_birth="13-08-2001", mark=75}

peopleArray = [person() for i in range(200)]

peopleArray[0].forename = "John"
peopleArray[0].surname = "Smith"
peopleArray[0].date_of_birth = "13-08-2001"
peopleArray[0].mark = 75

Create your own array of records using the record structures you created before:

  1. Create an array of driver records
  2. Create an array of pupils
  3. Create an array of pets

Task

Presentation Overview
Close
JB
Data types and structures
© 2020 - 2024 J Balfour
16:16 | 29-04-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files