Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> array pp 75</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Array                                                                 pp 75


 Define:  An array is a structured type consisting of a fixed number of
          elements which are all of the same type.  This type is called
          the 'Component Type'.

          The definition consists of the reserved word 'ARRAY' followed by
          the index type in square brackets, the reserved word 'OF', and
          terminated with the component type.


 Purpose: Provide a table of data items of the same type.


 Notes:   Each element can be explicitly accessed by an index into the array.
          The index can be any scalar type, placed in square brakets suffixed
          to the array identifier, and its type is called the 'index type'.

          Assignment is allowed between any two variables of identical type,
          so entire arrays can be copied with a single assignment statement.

          The R compiler directive controls the generation of code which will
          perform range checks on array index expressions at run time.  The
          active mode causes all index expressions to be checked against the
          limits of their index type.

 Usage:

 VAR
      Arrai    : Array [0..9] of String [80] ;

 BEGIN
      Arrai[1] := 'This is array #1' ;
      Arrai[2] := 'This is array #2' ;
      Arrai[3] := 'This is array #3' ;
 END.



 TYPE
      Players     = (P1,P2,P3,P4) ;
      Hand        = (One, Two, Pair, TwoPair, Three, Straight,
                     Flush, FullHouse, Four, StrFlush, RSF) ;
      LegalBid    = 1..200 ;
      Bid         = Array [Players] of LegalBid ;

 VAR
      Player      : Array [Players] of Hand ;
      Pot         : Bid ;

 BEGIN
      Player [P3] := FullHouse ;
      Pot [P3]    := 100 ;
      Player [P4] := Flush ;
      Pot [P4]    := 50 ;
 END.



 Multi-Dimensional Arrays                                              pp 76

 TYPE
      Card        = (Two, Three, Four, Five, Six, Seven, Eight,
                     Nine, Ten, Jack, Queen, King, Ace) ;
      Suit        = (Hearts, Spade, Clubs, Diamonds) ;
      AllCards    = Array [Suit] of Array [1..13] of Card ;

 VAR
      Deck        : AllCards ;


      A multi-dimensional array may be defined more conveniently by
      specifying the multiple indices thus:


 TYPE
      AllCards    = Array [Suit, 1..13] of Card ;


 TYPE
      Pupils            = String [20] ;
      Class             = Array [1..30] of Pupils ;
      School            = Array [1..99] of Class ;

 VAR
      J,P,Vacant        : Integer ;
      ClassA            : Class ;
      ClassB            : Class ;
      NewSchool         : School ;

 BEGIN
      ClassA [J]        := 'Peter' ;         { Name in class           }
      NewSchool [5][21] := 'Peter Brown' ;   { Name in school          }
      NewSchool [8,J]   := NewSchool [7,J] ; { J changed class         }
      ClassA [Vacant]   := ClassB [P] ;      { P changed class, number }
 END.



 Character Arrays                                                      pp 77

      Character arrays have one index and components of the standard scalar
      type 'CHAR'.  Character arrays may be though of as strings with a
      constant length.

      Character arrays may participate in string expressions, in which case
      the array is converted into a string of the length of the array.
      Arrays may be compared and manipulated in the same was as strings, and
      string constants may be assigned to character arrays as long as they
      are of the same length.  String variables and values computed from
      string expressions cannot be assigned to character arrays.

See Also: Array Constant Type Mem Port

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson