Summary:Data types and structures

From Higher Computing Science
Jump to: navigation, search

Data types

  • A string is an array of characters. Most languages treat strings differently from arrays, because text is commonly used and manipulated in a program.
  • An integer is a whole number stored as a positive binary number, or a two’s complement binary number (which allows storage of positive and negative numbers)

  • A real number is a simple data type stored as a floating point number – a number split into two parts
  • A real number is a number with a fractional part (or decimal place).
  • A real number is stored in parts called the mantissa and exponent. This kind of number is called a floating point number.
  • The mantissa is a number used to store the precision of the number. The number of bits reserved for the mantissa determines its precision.
  • The exponent is a number used to store the range of a number. The number of bits reserved for the exponent determines its range.
  • Floating point numbers can also used an extra bit called a signed bit to indicate whether a number is positive or negative

  • Boolean is a simple data type that is stored as either True or False
  • An array is an ordered sequence of simple data types, all of the same type
  • An array is used to store a list of items - a variable only stores one item.
  • Each item in the list is given an index number. Some languages index from 0 onwards, some index from 1. Haggis Pseudocode (which is used by the SQA in exams) is indexed from 0 onwards.
  • Arrays are a good way of storing lists as they can be processed using their index, making it easy to search and sort the list

  • A record is a data structure that contains values of different types, e.g. a record of type Person could consist of a name (string), address (string) and house number (integer)

Sequential files

  • Programs can access data from files. The process is the same in almost every programming language:
    • Open a file
    • For each line in the file:
      • Read line from file
    • Close file

  • Programs can also write data to files:
    • Open a file (create if file doesn’t exist)
    • For each item to be written to a file:
      • Write line to file
    • Close file
  • Multiple items can be read from each line, or written to one line of a file. Most files separate items using a comma.