Arrays, Tuples and Records

Cards (32)

  • Data structure
    A way of organizing and storing data in a computer so that it can be used efficiently
  • Elementary data types
    • integer
    • real
    • Boolean
    • char
  • Structured data types
    • string
    • array
    • record
  • Array
    A finite, ordered set of elements of the same type
  • An array has a specific number of elements and the elements are ordered (first, second, third, etc.)
  • Example array
    • myArray[51, 72, 35, 37, 0, 31]
  • x = myArray[2] assigns 35 to x
  • The RSPB organises a Big Garden Birdwatch to involve the public in counting the number of birds of different types they see in their gardens on a particular weekend
  • During 30-31 January 2016, more than 8 million birds were counted and reported
  • The scientists add all the sightings together, and once the data has been analysed they can discover trends and understand how different birds and other wildlife are faring
  • Example array of bird names
    • birdName["robin", "blackbird", "pigeon", "magpie", "bluetit", "thrush", "wren", "starling"]
  • birdName[2] = "pigeon"
  • numSpecies = len(birdName) assigns 8 to numSpecies
    1. dimensional arrays
    An array can have two or more dimensions. A two-dimension array is like a spreadsheet.
    1. dimensional array

    • numbers
    • 3 rows, 4 columns
  • Elements in the array
    Referred to by their row and column number, e.g. numbers[1,3]
    1. dimensional array "numbers"

    • Row 0: 2, Column 1, Column 2, Column 3
    • Row 1: 5, 6
    • Row 2: 9, 10, 11, 12
  • Value of numbers[2,3]
    12
  • Pseudocode algorithm to print quarterly sales
    1. For each of 3 sales staff (Anna, Bob, Carol)
    2. Print staff name
    3. For each quarter (0 to 3)
    4. Print quarter number and sales figure
    5. Calculate annual sales
  • The staff names are in a 1-dimensional array "staff"
  • The quarterly sales figures are in a 2-dimensional array "quartersales"
  • Arrays may have more than two dimensions. An n-dimensional array is a set of elements of the same type, indexed by n integers.
    1. dimensional array
    • x[4,5,2]
  • Tuple
    An ordered set of values, which could be elements of any type such as strings, integers or numbers, or even graphic images, sound files or arrays
  • Tuple
    • Unlike arrays, the elements do not have to be of the same type
    • A tuple, like a string, is immutable, which means that its elements cannot be changed, and you cannot dynamically add elements to or delete elements from a tuple
  • Tuple
    • pupil ("John", 78, "a")
  • Referring to individual elements of a tuple
    name pupil(0)
  • The statement pupil[0] is invalid
  • Record
    A record contains a number of fields, each holding one item of data
  • Record structure
    • ID
    • Firstname
    • Surname
    • DateOfBirth
    • Class
  • User-defined data type
    • studentType
    • integer ID
    • string firstname
    • string surname
    • date dateOfBirth
    • string class
  • Accessing fields in a record
    student.surname