IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help


Compound Data Types

Arrays in General

An ARRAY is an ordered collection of data where the order of input, and repetition of values can be stored. Multi-dimensioning possible.

a 1 dimensional array is sometimes termed a vector


   eg:      Type vector = array[1..5] of byte;

Generally, vectors (and arrays in general) are DEFINED in type statements, especially if the program will use lots (>1) of them

   syntax:  Type name = array[subscript_type] of element_type

   where:   subscript_type = an ORDINAL indexing system allowing us
                  to refer to each of the elements
               element_type = the style of data item being stored in each
                  of the memory cells

The indexing system must be ORDINAL, as at compile time, indexed locations in the array are allocated sequential memory addresses.

An example declaration and assignment:

      Const    maxPrisoner = 5;
      Type  prisoner = (fred, barney, wilma, bamBam);
            cellBlock = array [1..maxPrisoner] of prisoner;
      Var   A  :  cellBlock;
         A[1] := fred;
         A[2] := barney;
         A[5] := bamBam;
         A[3] := fred  {a neat trick as fred is now in 2 cells}

To 'give' or assign a type compatible array the values contained within another, we could either:

      for loop := 1 to maxPrisoner do
         B[loop] := A[loop] {one element of the source at a time}

or simply:

      B := A;  {the entire array assigned the entire value of the other}

With Type compatible arrays, the number of cells, and their data type must match

Vectors could also be DECLARED in a program:

      Var results : array [1..30] of real;

creates a memory cell, sub divided up into 30 neighbouring addresses - the entire memory area is called results, each part of the memory cell is addressed with the index number (eg: results[12])

Each of the results locations is designed to hold a single real number - any attempt to place an incompatible typed variable will result in a TYPE MISMATCH error.

CASE STUDY - a little more VECTOR education

A primitive markbook provides a useful model

      Const    numMarks     = 30;
      Type     marks        = real;
               markRange    = 1..numMarks;
               results      = array [markRange];
      Var      IPT, BIOL    : results;
               loop         : markRange;
               sum          : integer;
               for loop := 1 to numMarks do
                      IPT[loop] := 0;  {initialise IPT}

               for loop := 1 to numMarks do
                  begin           {get BIOL marks}
                      write('Type in a BIOL mark : ');

               sum := 0;
               for loop := 1 to numMarks do
                  sum := sum + BIOL[loop];
               writeln('The Average BIOL mark = ', sum/numMarks:0:2);

As has been previously stated, vectors can be declared of all types (with the exception of FILE).

To create a Multi-dimensional vector of sorts, one could declare an array of set as follows:

    Type  charset    = set of char;
          charVector = array[1..10] of charset;
    Var   thingo     : charVector;
          loop       : 1..10;
          for loop := 1 to 10 do
             thingo[loop] := []  {setting all vector cells to null sets}
          thingo[3] := thingo[3] + ['A'];
            {places a 'A' in the thingo cell 3 set}

This form of data storage is useful if a series of collections of distinct (ordinal) values are required

goto the 100 coin problem


a string is a vector of characters (either fixed or variable length - up to 255 characters long each)

   eg:   Var   message : string[30]
               message :=      'the hills are alive'

               writeln(message[2]);        ---> h

               for loop := 10 downto 1 do
                  write(message[loop]);    ---> sllih eht


    insert : 'add' characters to an existing string, pushing others out of the way in the process
         insert ( 'newstring' , destinationstring , startingpos )

         eg:          insert ('foot',message,5)
         result       message := 'the foothills are alive'
    delete : 'kill' characters in an existing string
         delete ( stringname , startingposition , numofchars )

         eg:          delete (message,11,5 )
         result       message := 'the hills live'
    copy : duplicate characters from a source string used as part of a string expression (ie. a function)
         copy ( sourcestring , startingpos , numofchars )

         eg:          newstring := copy(message,6,3)
         result       newstring := 'ill'
    length : find out the number of significant characters in a string used as part of an expression (ie. a function)
         length ( sourcestring )     

         eg.num := length (newstring)  result   num := 3
    position : to find the location of a substring within a string a function also
         pos ( substring , sourcestring )

         eg:          place := pos('hills',message)
         result:      place := 4
         eg:          place := pos('albatross',message)
         result:      place := 0        <-----substring not found
    converting numbers into strings - str
         suppose  Var    this : string[10];
                         num1 : real; num2 : integer;
                         num1 := 85.5;   num2 := 6;

         str(num1,this)        results in         this := '85.5'

         str(num2:4,this)      results in         this := '_ _ _ 6'

         str(num1:5:2,this)    results in         this := '85.50'
    converting stings into numbers - val
   suppose  Var    this, that, theother : string[10];
                      err : byte;  answer1 : integer;  answer2 : real
                   this     := '64.3';
                   that      := '42 is the answer';
                   theother := '_ _ _ -42'

        val(this,answer2,err)    results in  answer2 := 64.2; err := 0
        val(that,answer1,err)    results in  answer1 unassigned; err :=4
        val(theother,answer1,err)   gives    answer1 := -42;  err := 0

Ideally, user input is accepted in string format, and parsed to turn it into numeric if required - this allows for some powerful error trapping

String Input/Output

Strings can be input by Read[ln] and output by Write[ln]

When reading in strings, user input is truncated to fit into the defined string length.

String input is terminated using a return

Read as opposed to readln can cause problems if the input is longer than the receiving string - the extra characters will be left on the keyboard buffer and may become part of the next read

String Case Study:

      Type        strings : string[80];

      Function mystery (g: strings) : strings;
         var   loop, len       : 0..80;
               copy            : char;
               len := length(g);
               for loop := 1 to len div 2 do
                      copy := g[loop];
                      g[loop] := g[len - loop + 1]
                      g[len - loop + 1] := copy
               mystery := g



A multi-dimensioned array can be visualized as arrays within the cells of other arrays. A Vector is a 1 dimensioned array (length) - if each of it's cells is subdivided, then the result is a 2 dimensional array (length, width). If we further sub-divide the sub-dived vector cells, we get a 3-D array (length, width, depth).

Visualizing higher dimensions becomes a little messy, but programmers frequently utilize multiple dimension arrays for the organised storage of their data.

   example:    a timetable

         Program Multi-Dim;

         Type     days      = 1..10;
                  periods   = 1..7;
                  subjects  = string[5];

         Var      timetable    :  array [days, periods] of subjects;
                  period       :  periods;
                  day          :  days;

            {timetable[1]  refers to DAY1's timetable<--- partial ref}
            {timetable[3][3] refers to DAY3 PERIOD3  <--- layered ref}

            timetable[1,3] := 'geog';              { <--- flat ref}

            {output the timetable for day2}

            for period := 1 to 7 do

            {output the whole timetable}

            for day := 1 to 10 do
                  for period := 1 to 7 do
                      write (timetable[day,period]:10);

Array cell types can be any type (except FILE)

An alternative definition might read:

               timetable : array(days) of array(periods) of subjects

A possible advantage : allows you to refer to individual rows collectively.

Compound Data Structures - ARRAYS


©Copyright t 1992..2018+. Edition 26.150117
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License