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

  1.                 CHAPTER 8 - Scalars, subranges, and sets.
  2.  
  3.  
  4.                               PASCAL SCALARS
  5.  
  6.             A scalar,  also called an enumerated type,  is a list of 
  7.  
  8.         values  which a variable of that type may assume.   Look  at 
  9.  
  10.         the  Pascal program ENTYPES for an example of some  scalars.  
  11.  
  12.         The  first  type declaration defines "days" as being a  type 
  13.  
  14.         which can take on any one of seven  values.   Since,  within 
  15.  
  16.         the  VAR declaration,  "day" is assigned the type of "days", 
  17.  
  18.         then  "day" is a variable which can assume any one of  seven 
  19.  
  20.         different values.   Moreover "day" can be assigned the value 
  21.  
  22.         "mon",  or  "tue",  etc.   This makes the program easier  to 
  23.  
  24.         follow and understand.  Internally, Pascal does not actually 
  25.  
  26.         assign the value "mon" to the variable "day", but it uses an 
  27.  
  28.         integer  representation  for each of  the  names.   This  is 
  29.  
  30.         important to understand because you need to realize that you 
  31.  
  32.         cannot print out "mon",  "tue",  etc., but can only use them 
  33.  
  34.         for indexing control statements.
  35.  
  36.             The   second   line  of  the  type  definition   defines 
  37.  
  38.         "time_of_day"  as another scalar which can have any of  four 
  39.  
  40.         different values,  namely those listed.  The variable "time" 
  41.  
  42.         can only be assigned one of four values since it is  defined 
  43.  
  44.         as  the  type "time_of_day".   It should be clear that  even 
  45.  
  46.         though   it  can  be  "morning",   it  cannot  be   assigned 
  47.  
  48.         "morning_time"  or  any other variant spelling  of  morning, 
  49.  
  50.         since  it  is simply another identifier which must  have  an 
  51.  
  52.         exact spelling to be understood by the compiler.
  53.  
  54.             Several  real  variables  are defined  to  allow  us  to 
  55.  
  56.         demonstrate the use of the scalar variables.   After writing 
  57.  
  58.         a header for our output,  the real variables are initialized 
  59.  
  60.         to  some values that are probably not real life values,  but 
  61.  
  62.         will serve to illustrate the scalar variable.
  63.  
  64.                         A BIG SCALAR VARIABLE LOOP
  65.  
  66.             The  remainder  of the program is one large  loop  being 
  67.  
  68.         controlled  by the variable "day" as it goes through all  of 
  69.  
  70.         its values,  one at a time.   Note that the loop could  have 
  71.  
  72.         gone  from  "tue" to "sat" or whatever portion of the  range 
  73.  
  74.         desired, it does not have to go through all of the values of 
  75.  
  76.         "day".  Using "day" as the case variable, the name of one of 
  77.  
  78.         the days of the week is written out each time we go  through 
  79.  
  80.         the  loop.   Another  loop controlled by "time" is  executed 
  81.  
  82.         four  times,  once for each value of "time".   The two  CASE 
  83.  
  84.         statements  within the inner loop are used to calculate  the 
  85.  
  86.         total pay rate for each time period and each day.   The data 
  87.  
  88.         is  formatted carefully to make a nice looking table of  pay 
  89.  
  90.         rates as a function of "time" and "day".
  91.  
  92.  
  93.  
  94.  
  95.  
  96.                                  Page 35
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                 CHAPTER 8 - Scalars, subranges, and sets.
  107.  
  108.  
  109.             Take  careful  notice  of  the  fact  that  the   scalar 
  110.  
  111.         variables never entered into the calculations, and they were 
  112.  
  113.         not printed out.  They were only used to control the flow of 
  114.  
  115.         logic. It was much neater than trying to remember that "mon" 
  116.  
  117.         is represented by a 0, "tue" is represented by a 1, etc.  In 
  118.  
  119.         fact, those numbers are used for the internal representation 
  120.  
  121.         of  the scalars but we can relax and let Pascal worry  about 
  122.  
  123.         the internal representation of our scalars.
  124.  
  125.                         LETS LOOK AT SOME SUBRANGES
  126.  
  127.             Examine   the   program  SUBRANGE  for  an  example   of 
  128.  
  129.         subranges.   It  may be expedient to define  some  variables 
  130.  
  131.         that  only  cover a part of the full range as defined  in  a 
  132.  
  133.         scalar  type.   Notice that "days" is declared a scalar type 
  134.  
  135.         as in the last program,  and "work" is declared a type  with 
  136.  
  137.         an  even  more restricted range.   In the  VAR  declaration, 
  138.  
  139.         "day"  is once again defined as the days of the week and can 
  140.  
  141.         be  assigned any of the days by the program.   The  variable 
  142.  
  143.         "workday",  however,  is assigned the type "work",  and  can 
  144.  
  145.         only  be  assigned  the days "mon"  through  "fri".   If  an 
  146.  
  147.         attempt  is  made  to assign "workday" the  value  "sat",  a 
  148.  
  149.         runtime  error  will  be  generated.   A  carefully  written 
  150.  
  151.         program  will  never  attempt  that,  and  it  would  be  an 
  152.  
  153.         indication  that something is wrong with either the  program 
  154.  
  155.         or the data.   This is one of the advantages of Pascal  over 
  156.  
  157.         older languages.
  158.  
  159.             Further examination will reveal that "index" is assigned 
  160.  
  161.         the  range of integers from 1 through 12.   During execution 
  162.  
  163.         of the program,  if an attempt is made to assign "index" any 
  164.  
  165.         value  outside  of  that range,  a run time  error  will  be 
  166.  
  167.         generated.   Suppose  the variable "index" was  intended  to 
  168.  
  169.         refer  to  your  employees,  and you have only  12.   If  an 
  170.  
  171.         attempt was made to refer to employee number 27, or employee 
  172.  
  173.         number -8,  there is clearly an error somewhere in the  data 
  174.  
  175.         and  you  would want to stop running the payroll to fix  the 
  176.  
  177.         problem.  Pascal would have saved you a lot of grief.
  178.  
  179.                    SOME STATEMENTS WITH ERRORS IN THEM.
  180.  
  181.             In  order to have a program that would  compile  without 
  182.  
  183.         errors,  and  yet  show some errors,  the first part of  the 
  184.  
  185.         program  is  not really a part of the program  since  it  is 
  186.  
  187.         within a comment area.  This is a trick to remember when you 
  188.  
  189.         are debugging a program, a troublesome part can be commented 
  190.  
  191.         out  until  you are ready to include it in  the  rest.   The 
  192.  
  193.         errors are self explanatory.
  194.  
  195.             There  are  seven assignment statements as  examples  of 
  196.  
  197.         subrange  variable use.   Notice that the variable "day" can 
  198.  
  199.  
  200.  
  201.                                  Page 36
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.                 CHAPTER 8 - Scalars, subranges, and sets.
  212.  
  213.  
  214.         always  be  assigned  the  value  of  either  "workday"   or 
  215.  
  216.         "weekend",  but  the  reverse is not true because "day"  can 
  217.  
  218.         assume values that would be illegal for the others.
  219.  
  220.                         THREE VERY USEFUL FUNCTIONS
  221.  
  222.             The last section of the example program demonstrates the 
  223.  
  224.         use  of three very important functions when  using  scalars.  
  225.  
  226.         The  first is the "succ" function that returns the value  of 
  227.  
  228.         the successor to the scalar used as an argument, that is the 
  229.  
  230.         next value.   If the argument is the last value,  a run time 
  231.  
  232.         error  is  generated.   The  next  function  is  the  "pred" 
  233.  
  234.         function that returns the predecessor to the argument of the 
  235.  
  236.         function.   Finally  the  "ord" function which  returns  the 
  237.  
  238.         ordinal  value of the scalar.   All scalars have an internal 
  239.  
  240.         representation starting at 0 and increasing by one until the 
  241.  
  242.         end is reached.   In our example program, "ord(day)" is 5 if 
  243.  
  244.         "day"  has been assigned "sat",  but "ord(weekend)" is 0  if 
  245.  
  246.         "weekend" has been assigned "sat".   As you gain  experience 
  247.  
  248.         in programming with scalars and subranges,  you will realize 
  249.  
  250.         the value of these three functions.
  251.  
  252.             A  few more thoughts about subranges are in order before 
  253.  
  254.         we go on to another topic.   A subrange is always defined by 
  255.  
  256.         two  predefined  constants,  and  is always  defined  in  an 
  257.  
  258.         ascending order.   A variable defined as a subrange type  is 
  259.  
  260.         actually  a  variable defined with a restricted  range,  and 
  261.  
  262.         should  be  used as often as possible in  order  to  prevent 
  263.  
  264.         garbage  data.   There are actually very few variables  ever 
  265.  
  266.         used  that cannot be restricted by some amount.   The limits 
  267.  
  268.         may give a hint at what the program is doing and can help in 
  269.  
  270.         understanding  the program operation.   Subrange  types  can 
  271.  
  272.         only be constructed using the simple types,  INTEGER,  CHAR, 
  273.  
  274.         or scalar.
  275.  
  276.                                    SETS
  277.  
  278.             Now for a new topic, sets.  Examining the example Pascal 
  279.  
  280.         program  SETS will reveal some sets.   A scalar variable  is 
  281.  
  282.         defined first, in this case the scalar type named "goodies".  
  283.  
  284.         A  set  is  then  defined with the  reserved  words  SET  OF 
  285.  
  286.         followed by a predefined scalar type.  Several variables are 
  287.  
  288.         defined   as   sets  of  "treat",   after  which  they   can 
  289.  
  290.         individually be assigned portions of the entire set.
  291.  
  292.             Consider  the variable "ice_cream_cone" which  has  been 
  293.  
  294.         defined as a set of type "treat".  This variable is composed 
  295.  
  296.         of as many elements of "goodies" as we care to assign to it.  
  297.  
  298.         In   the  program,   we  define  it  as  being  composed  of 
  299.  
  300.         "ice_cream",   and  "cone".   The  set  "ice_cream_cone"  is 
  301.  
  302.         therefore composed of two elements,  and it has no numerical 
  303.  
  304.  
  305.  
  306.                                  Page 37
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.                 CHAPTER 8 - Scalars, subranges, and sets.
  317.  
  318.  
  319.         or   alphabetic   value  as  most  other   variables   have.  
  320.  
  321.         Continuing  in the program,  you will see 4  more  delicious 
  322.  
  323.         deserts  defined as sets of their components.   Notice  that 
  324.  
  325.         the banana split is first defined as a range of terms,  then 
  326.  
  327.         another  term is added to the group.   All five are combined 
  328.  
  329.         in  the set named "mixed",  then "mixed" is subtracted  from 
  330.  
  331.         the entire set of values to form the set of ingredients that 
  332.  
  333.         are not used in any of the deserts.  Each ingredient is then 
  334.  
  335.         checked  to see if it is IN the set of  unused  ingredients, 
  336.  
  337.         and printed out if it is.  Running the program will reveal a 
  338.  
  339.         list of unused elements.
  340.  
  341.             In this example,  better programming practice would have 
  342.  
  343.         dictated   defining   a  new   variable,   possibly   called 
  344.  
  345.         "remaining" for the ingredients unused.  It was desirable to 
  346.  
  347.         illustrate  that "mixed" could be assigned a value based  on 
  348.  
  349.         subtracting itself from the entire set, so the poor variable 
  350.  
  351.         name was used.
  352.  
  353.             This  example  resulted  in some  nonsense  results  but 
  354.  
  355.         hopefully it led your thinking toward the fact that sets can 
  356.  
  357.         be  used for inventory control,  possibly a parts allocation 
  358.  
  359.         scheme, or some other useful system.
  360.  
  361.                             SEARCHING WITH SETS
  362.  
  363.             The Pascal program FINDCHRS is more useful than the last 
  364.  
  365.         one.  In it we start with a short sentence and search it for 
  366.  
  367.         all lower case alphabetic letters and write a list of  those 
  368.  
  369.         used.  Since we are using a portion of the complete range of 
  370.  
  371.         CHAR,  we do not need to define a scalar before defining the 
  372.  
  373.         set,  we  can define the set using the range 'a'..'z'.   The 
  374.  
  375.         set  "data_set" is assigned the value of no elements in  the 
  376.  
  377.         first statement of the program,  and the print string, named 
  378.  
  379.         "print_group",  is set to blank in the next.   The  variable 
  380.  
  381.         "storage" is assigned the sentence to search, and the search 
  382.  
  383.         loop  is  begun.   Each time through the loop,  one  of  the 
  384.  
  385.         characters  is  checked.   It is either declared as  a  non-
  386.  
  387.         lower-case character,  is a repeat of one already found,  or 
  388.  
  389.         is a new one added to the list.
  390.  
  391.             You  are  left to decipher the details of  the  program, 
  392.  
  393.         which  should be no problem since there is nothing new here.  
  394.  
  395.         Run  the  program and observe how the list  grows  with  new 
  396.  
  397.         letters as the sentence is scanned.
  398.  
  399.                            PROGRAMMING EXERCISE
  400.  
  401.         1. Modify FINDCHRS to search for upper-case letters.
  402.  
  403.  
  404.  
  405.  
  406.                                  Page 38
  407.  
  408.