home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP6.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  17.0 KB  |  389 lines

  1.             CHAPTER 6 - Arrays, types, constants, and labels
  2.  
  3.  
  4.                                   ARRAYS
  5.  
  6.             Having covered nearly all of the programming statements, 
  7.  
  8.         we  must  now  go  back and fill in some gaps  in  our  data 
  9.  
  10.         definition.
  11.  
  12.            One  of  the most useful data structures  is  the  ARRAY, 
  13.  
  14.         which is,  in the simplest terms,  a group of many identical 
  15.  
  16.         terms.   Lets go directly to an example to see what an array 
  17.  
  18.         looks  like.   Edit the Pascal program ARRAYS and notice the 
  19.  
  20.         third  line  starting  with  the  word  "automobiles".   The 
  21.  
  22.         variable "automobiles" is defined as an integer variable but 
  23.  
  24.         in addition,  it is defined to have twelve different integer 
  25.  
  26.         variables,    namely    "automobile[1]",    "automobile[2]", 
  27.  
  28.         "automobile[3]", .. "automobile[12]".  The square braces are 
  29.  
  30.         used in Pascal to denote a subscript for an array  variable.  
  31.  
  32.         The  array  definition  given  in line  3  is  the  standard 
  33.  
  34.         definition for an array,  namely a variable name followed by 
  35.  
  36.         a  colon and the reserved word ARRAY,  with the range of the 
  37.  
  38.         array given in square brackets followed by another  reserved 
  39.  
  40.         word OF and finally the type of variable.
  41.  
  42.             In using the elements of the array in a program, each of 
  43.  
  44.         the elements of the array are required to be used in exactly 
  45.  
  46.         the same manner as any simple variable having the same type.  
  47.  
  48.         Each  time  one of the variables is used,  it must have  the 
  49.  
  50.         subscript  since the subscript is now part of  the  variable 
  51.  
  52.         name.   The subscript moreover,  must be of the type used in 
  53.  
  54.         the definition and it must be within the range defined or it 
  55.  
  56.         will be listed as an error.
  57.  
  58.             Now  consider the program itself.   As "index" is varied 
  59.  
  60.         from 1 to 12, the range of the variable "automobile", the 12 
  61.  
  62.         variables  are set to the series of values 11  to  22.   Any 
  63.  
  64.         integer values could be used, this was only a convenient way 
  65.  
  66.         to set the values to some numbers.   With the values stored, 
  67.  
  68.         a  header  is  now printed and finally the  list  of  values 
  69.  
  70.         contained in the array.   Note carefully that,  although the 
  71.  
  72.         subscripts are limited to 1 through 12, the values stored in 
  73.  
  74.         each  of  the 12 variables are limited only by the range  of 
  75.  
  76.         integers,  namely -32768 to 32767.  Review this material and 
  77.  
  78.         this program as long as needed to fully understand it, as it 
  79.  
  80.         is very important.
  81.  
  82.                            DOUBLY INDEXED ARRAYS
  83.  
  84.             After understanding the above,  load the program ARRAYS2 
  85.  
  86.         to see the next level of complexity of arrays.  You will see 
  87.  
  88.         that "checkerboard" is defined as an array from 1 to 8,  but 
  89.  
  90.         instead of it being a simple data type, it is itself another 
  91.  
  92.         array from 1 to 8 of INTEGER.   The variable  "checkerboard" 
  93.  
  94.  
  95.  
  96.                                 Page 27
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.             CHAPTER 6 - Arrays, types, constants, and labels
  107.  
  108.  
  109.         is  actually  composed  of 8 elements,  each of which  is  8 
  110.  
  111.         elements,  leading to a total of 64 elements,  each of which 
  112.  
  113.         is  a  simple INTEGER variable.   This is  called  a  doubly 
  114.  
  115.         subscripted  array  and it can be envisioned in exactly  the 
  116.  
  117.         same  manner  as a real checker board,  an 8  by  8  matrix.  
  118.  
  119.         Another  way to achieve the same end is to define the double 
  120.  
  121.         array  as in the next line of the program where  "value"  is 
  122.  
  123.         defined as a total of 64 elements.
  124.  
  125.             To use either of the two variables in a program, we must 
  126.  
  127.         add  two subscripts to the variable name to tell the program 
  128.  
  129.         which  element of the 64 we desire to  use.   Examining  the 
  130.  
  131.         program will reveal two loops,  one nested within the other, 
  132.  
  133.         and both ranging in value from 1 to 8.  The two loop indices 
  134.  
  135.         can  therefore  be used as subscripts of the  defined  array 
  136.  
  137.         variables.   The  variable "checkerboard" is subscripted  by 
  138.  
  139.         both  of  the loop indices and each of the 64  variables  is 
  140.  
  141.         assigned a value as a function of the indices.  The assigned 
  142.  
  143.         value  has  no real meaning other than to illustrate to  you 
  144.  
  145.         how  it is done.   Since the value of "checkerboard" is  now 
  146.  
  147.         available,  it is used to define some values to be used  for 
  148.  
  149.         the variable "value".
  150.  
  151.             After  defining all of those variables,  and you  should 
  152.  
  153.         understand  that we have defined a total of 128 variables in 
  154.  
  155.         the double loop,  they can be printed out.  The next section 
  156.  
  157.         of  the  program does just that,  by  using  another  doubly 
  158.  
  159.         nested  loop,  with a WRITE statement in the  center.   Each 
  160.  
  161.         time  we  go  through the center of the loop we tell  it  to 
  162.  
  163.         print  out  one of the 64 variables  in  the  "checkerboard" 
  164.  
  165.         matrix  with the indices "index" and "count" defining  which 
  166.  
  167.         of  the variables to write each time.   Careful study of the 
  168.  
  169.         loop should reveal its exact operation.
  170.  
  171.             After  printing out the matrix defined by  the  variable 
  172.  
  173.         "checkerboard"  we  still  have the matrix  defined  by  the 
  174.  
  175.         variable  "value"  intact  (In fact,  we still have  all  of 
  176.  
  177.         "checkerboard"  available because we haven't changed any  of 
  178.  
  179.         it).   Before  printing out the matrix defined  by  "value", 
  180.  
  181.         let's  change  a few of the elements just to see how  it  is 
  182.  
  183.         done.   The  next  three  lines simply change three  of  the 
  184.  
  185.         variables  to illustrate that you can operate on all of  the 
  186.  
  187.         matrix  in  loops,  or on any part of the matrix  in  simple 
  188.  
  189.         assignment statements.  Notice especially the third line, in 
  190.  
  191.         which  "value[3,6]" (which was just set to the value of  3), 
  192.  
  193.         is used as a subscript.  This is perfectly legal since it is 
  194.  
  195.         defined as a simple integer variable and is within the range 
  196.  
  197.         of 1 to 8,  which is the requirement for a subscript of  the 
  198.  
  199.         variable  "value".   The  last  part of the  program  simply 
  200.  
  201.         prints  out  the 64 values of the variable "value"   in  the 
  202.  
  203.  
  204.  
  205.  
  206.  
  207.                                 Page 28
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.             CHAPTER 6 - Arrays, types, constants, and labels
  218.  
  219.  
  220.         same manner as above.  Notice that when you run the program, 
  221.  
  222.         the three values are in fact changed as expected.
  223.  
  224.                             ARRAYS ARE FLEXIBLE
  225.  
  226.             A  few  more words about arrays before we  go  on.   The 
  227.  
  228.         arrays  in the last program were both defined to be  square, 
  229.  
  230.         namely  8 by 8 but that choice was  purely  arbitrary.   The 
  231.  
  232.         subscripts were chosen to go from 1 to 8 but they could have 
  233.  
  234.         been  chosen to go from 101 to 108 or any other range needed 
  235.  
  236.         to clearly define the problem at hand.  And, as you may have 
  237.  
  238.         guessed,  you are not limited to a doubly subscripted matrix 
  239.  
  240.         but you can define a variable with as many subscripts as you 
  241.  
  242.         need  to  achieve your desired end.   There is  a  practical 
  243.  
  244.         limit  to  the  number of subscripts because  you  can  very 
  245.  
  246.         quickly  use up all of your available memory with one  large 
  247.  
  248.         subscripted variable.
  249.  
  250.                             THE TYPE DEFINITION
  251.  
  252.             Now  that  you understand arrays,  lets look at  a  more 
  253.  
  254.         convenient  way to define them by examining the Pascal  file 
  255.  
  256.         TYPES.   You  will notice a new section at the beginning  of 
  257.  
  258.         the listing with the heading TYPE.  TYPE is another reserved 
  259.  
  260.         word  which  is used at the beginning of a section  used  to 
  261.  
  262.         define  "user-defined  types".   Beginning with  the  simple 
  263.  
  264.         predefined TYPES we studied earlier, we can build up as many 
  265.  
  266.         new  types  as  we need and they can be  as  complex  as  we 
  267.  
  268.         desire.   The  six names (from "array_def" to "boat") in the 
  269.  
  270.         TYPE section are not variables,  but are defined to be TYPES 
  271.  
  272.         and  can be used in the same manner as  can  INTEGER,  BYTE, 
  273.  
  274.         REAL, etc.
  275.  
  276.             This  is a very difficult concept,  but a very important 
  277.  
  278.         one.   The Pascal compiler is very picky about the  variable 
  279.  
  280.         types  you  use in the program,  doing lots of  checking  to 
  281.  
  282.         insure  that  you do not use the wrong type anywhere in  the 
  283.  
  284.         program.   Because  it is picky,  you could do  very  little 
  285.  
  286.         without  the  ability to define new types when  needed,  and 
  287.  
  288.         that is the reason that you can define new types to solve  a 
  289.  
  290.         particular problem.
  291.  
  292.             Some of these types are used in the VAR declaration part 
  293.  
  294.         of the program.  Notice that since "airplane" is an array of 
  295.  
  296.         "dog_food"  and  "dog_food" is in turn an array of  BOOLEAN, 
  297.  
  298.         then  "airplane" defines a doubly  subscripted  array,  each 
  299.  
  300.         element being a boolean variable.   This does not define any 
  301.  
  302.         variables, only a TYPE, which can be used in a VAR to define 
  303.  
  304.         a matrix of boolean variables.   This is in fact done in the 
  305.  
  306.         definition of "puppies", which is an array composed of 72 (6 
  307.  
  308.         times 12) boolean variables.  In the same manner, "stuff" is 
  309.  
  310.  
  311.  
  312.                                 Page 29
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.             CHAPTER 6 - Arrays, types, constants, and labels
  323.  
  324.  
  325.         composed of an array of 14 variables,  each being an integer 
  326.  
  327.         variable.   The  elements  of the  array  are,  "stuff[12]", 
  328.  
  329.         "stuff[13]",  ..  "stuff[25]".  Notice also that "stuff2" is 
  330.  
  331.         also defined in exactly the same manner and is also composed 
  332.  
  333.         of 14 variables.
  334.  
  335.             Careful  inspection  will  reveal that  "kitties"  is  a 
  336.  
  337.         variable  which  has the same definition as  "puppies".   It 
  338.  
  339.         would  probably be poor programming practice to define  them 
  340.  
  341.         in  different  manners  unless they  were  in  fact  totally 
  342.  
  343.         disassociated.   In  this  example  program,  it  serves  to 
  344.  
  345.         illustrate  some  of  the  ways user-defined  types  can  be 
  346.  
  347.         defined.
  348.  
  349.             In  a tiny program like this example,  the value of  the 
  350.  
  351.         TYPE declaration part cannot be appreciated,  but in a large 
  352.  
  353.         program  with many variables,  the TYPE declaration  can  be 
  354.  
  355.         used to great advantage.  This will be illustrated later.
  356.  
  357.                          THE CONSTANT DECLARATION
  358.  
  359.             Examining  the Pascal example program CONSTANT will give 
  360.  
  361.         us an example of a constant definition.   The reserved  word 
  362.  
  363.         CONST is the beginning of the section that is used to define 
  364.  
  365.         constants  that can be used anyplace in the program as  long 
  366.  
  367.         as  they  are  consistent  with  the  required  data  typing 
  368.  
  369.         limitations.   In  this example,  "max_size" is defined as a 
  370.  
  371.         constant  with the value of 12.  This is not a variable  and 
  372.  
  373.         cannot  be  changed  in the program,  but is  still  a  very 
  374.  
  375.         valuable  number.   For  the  moment  ignore  the  next  two 
  376.  
  377.         constant definitions.   As we inspect the TYPE declarations, 
  378.  
  379.         we see two user-defined types,  both of which are arrays  of 
  380.  
  381.         size  1 to 12 since "max_size" is defined as 12.   Then when 
  382.  
  383.         we get to the VAR declaration part,  we find five  different 
  384.  
  385.         variables,  all  defined  as arrays from 1 to 12  (some  are 
  386.  
  387.         INTEGER and some are CHAR).   When we come to the program we 
  388.  
  389.         find  that  it is one big loop which we go through 12  times 
  390.  
  391.         because the loop is executed "max_size" times.
  392.  
  393.             In the above definition,  there seems to be no advantage 
  394.  
  395.         to  using the constant,  and there is none,  until you  find 
  396.  
  397.         that  for some reason you wish to increase the range of  all 
  398.  
  399.         arrays from 12 to 18.   In order to do so,  you only need to 
  400.  
  401.         redefine the value of the constant, recompile, and the whole 
  402.  
  403.         job  is done.   Without the constant definition,  you  would 
  404.  
  405.         have had to change all TYPE declarations and the upper limit 
  406.  
  407.         of the loop in the program.  Of course that would not be too 
  408.  
  409.         bad in the small example program,  but could be a real  mess 
  410.  
  411.         in  a 2000 line program,  especially if you missed  changing 
  412.  
  413.         one  of the 12's to an 18.   That would be a good example of 
  414.  
  415.         data in and garbage out.   That should give you a good  idea 
  416.  
  417.  
  418.  
  419.                                 Page 30
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.             CHAPTER 6 - Arrays, types, constants, and labels
  430.  
  431.  
  432.         of  what  the constant can be used for,  and as you  develop 
  433.  
  434.         good  programming  techniques,  you will  use  the  constant 
  435.  
  436.         declaration to your advantage.
  437.  
  438.                       THE TURBO PASCAL TYPED CONSTANT
  439.  
  440.             We   skipped   over  the  second  and   third   constant 
  441.  
  442.         declaration  for a very good reason,  they are not  constant 
  443.  
  444.         declarations.   TURBO  Pascal has defined,  as an extension, 
  445.  
  446.         the "typed constant".  Using the syntax shown, "index_start" 
  447.  
  448.         is defined as an INTEGER variable and is initialized to  the 
  449.  
  450.         value  of  49.   This is a true variable and can be used  as 
  451.  
  452.         such  in the program.   The same effect can be  achieved  by 
  453.  
  454.         simply  defining  "index_start"  as an INTEGER  in  the  VAR 
  455.  
  456.         declaration  part  and setting it to the value of 49 in  the 
  457.  
  458.         program itself.  Since it does not really fit the definition 
  459.  
  460.         of  a  constant,  it's  use is discouraged  until  you  gain 
  461.  
  462.         experience  as  a Pascal programmer.   Until  then  it  will 
  463.  
  464.         probably  only  be  confusing  to  you.    In  like  manner, 
  465.  
  466.         "check_it_out"  is  a  boolean variable initialized  to  the 
  467.  
  468.         value "true".  It is not a constant.
  469.  
  470.                            THE LABEL DECLARATION
  471.  
  472.             Finally,  the example program LABELS will illustrate the 
  473.  
  474.         use  of  labels.   In the Pascal definition,  a LABEL  is  a 
  475.  
  476.         number from 0 to 9999 that is used to define a point in  the 
  477.  
  478.         program  to  which  you wish to jump.   All labels  must  be 
  479.  
  480.         defined  in the LABEL definition part of the program  before 
  481.  
  482.         they can be used.   Then a new reserved word GOTO is used to 
  483.  
  484.         jump to that point in the program.   The best way to see how 
  485.  
  486.         the  GOTO  is  used with labels is to  examine  the  program 
  487.  
  488.         before you.   TURBO Pascal has an extension for labels.  Any 
  489.  
  490.         valid identifier, such as used for variables, can be used as 
  491.  
  492.         a label in addition to the values from 0 to 9999.  These are 
  493.  
  494.         illustrated in the example program.
  495.  
  496.                              THE PACKED ARRAY
  497.  
  498.             When  Pascal  was first defined in  1971,  many  of  the 
  499.  
  500.         computers in use at that time used very large words, 60 bits 
  501.  
  502.         being  a typical word size.   Memory was very expensive,  so 
  503.  
  504.         large memories were not too common.   A Pascal program  that 
  505.  
  506.         used  arrays  was inefficient because only one variable  was 
  507.  
  508.         stored  in each word.   Most of the bits in each  word  were 
  509.  
  510.         totally  wasted,  so  the PACKED ARRAY was defined in  which 
  511.  
  512.         several  variables  were stored in each  word.   This  saved 
  513.  
  514.         storage space but took extra time to unpack each word to use 
  515.  
  516.         the data.  The programmer was given a choice of using a fast 
  517.  
  518.         scheme  that wasted memory,  the ARRAY,  or a slower  scheme 
  519.  
  520.         that used memory more efficiently, the PACKED ARRAY.
  521.  
  522.  
  523.  
  524.                                 Page 31
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.             CHAPTER 6 - Arrays, types, constants, and labels
  535.  
  536.  
  537.  
  538.             The modern microcomputer has the best of both schemes, a 
  539.  
  540.         short word, usually 16 bits, and a large memory.  The PACKED 
  541.  
  542.         ARRAY  is therefore not even implemented in  many  compilers 
  543.  
  544.         and will be ignored during compilation.
  545.  
  546.  
  547.                            PROGRAMMING EXERCISES
  548.  
  549.         1.  Write  a program to store the integers 201 to 212 in  an 
  550.  
  551.             array then display them on the monitor.
  552.  
  553.         2.  Write a program to store a 10 by 10 array containing the 
  554.  
  555.             products  of  the indices,  therefore  a  multiplication 
  556.  
  557.             table. Display the matrix on the video monitor.
  558.  
  559.         3.  Modify  the program in 2 above to include a constant  so 
  560.  
  561.             that  by simply changing the constant,  the size of  the 
  562.  
  563.             matrix and the range of the table will be changed.
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                 Page 32
  599.  
  600.