Data Types and Structures

Simple Data Types

Data is stored in computer memory and the translation program has to decide where to store the data and how much space to reserve, so the program code must indicate what type of data is to be stored.

Simple data types are typically associated with storing a single item of data in a variable.

Structures

A data structure is a means of organising and storing a collection of related data so that it can be retrieved and manipulated within a program. Data structures like arrays, records and files are composite data objects made up of simple data types.

Arrays

Arrays are used for storing lists of items typically of the same data type.  They are known as indexed lists because every item in the array is referenced (accessed) by its index position.   The size of the array (the number of items in it) often has to be specified at the start of the program before it can be used.

Examples

names = []          creates an empty array in Python

marks[] * 20 As Integer          reserves space for 20 marks in python

Parallel Arrays

So far in National 5 and Higher, you have handled tables of data using parallel arrays. This is exemplified by the storage of some qualifying race heats data shown below in 3 separate parallel arrays.

Python Example

Records

Records in databases are used to store all the details on one person or thing. In programming, we create a Record structure to do the same. Records allow us to store different data types within the one data structure.

An example of a record using Pseudocode is:

RECORD student IS {name: STRING, surname: STRING, age: INTEGER, height: FLOAT}

An example of a record using python is:

Array of Records

A more intuitive way of storing the information would be to use an array of records. This means having just one array, but each array entry is actually a record.  This method keeps all related data together and organised in a way which is more familiar to us. It also removes those difficulties of adding and deleting records.

An example of an array record using Pseudocode is:

student_array=[student(“”,””,0,0.0)for counter in range(5)]

An example of an array record using python is:

Advantages of Array of Records

  • An array of records stores related data together within the same data structure.  
  • Passing data to sub-programs is easier with an array of records as only one data structure (rather than multiple) need be passed. 
  • There is a raised potential for errors when working with parallel arrays as all must be manipulated consistently.