home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP08.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  11.8 KB  |  295 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 8
  5.                                      SCALARS, SUBRANGES, AND SETS
  6.  
  7.  
  8. PASCAL SCALARS
  9. _________________________________________________________________
  10.  
  11. A scalar, also called an enumerated type, is a    ===============
  12. list of values which a variable of that type may    ENTYPES.PAS
  13. assume.  Examine the Pascal program ENTYPES.PAS   ===============
  14. for an example of some scalars.  The first type
  15. declaration defines Days as being a type which
  16. can take on any one of seven values.  Since, within the var
  17. declaration, Day is assigned the type of Days, Day is then a
  18. variable which can assume any one of seven different values. 
  19. Moreover Day can be assigned the value Mon, or Tue, etc., which is
  20. considerably clearer than using 0 to represent Monday, 1 for
  21. Tuesday, etc.  This makes the program easier to follow and
  22. understand.  
  23.  
  24. Internally, Pascal does not actually assign the value Mon to the
  25. variable Day, but it uses an integer representation for each of the
  26. names.  This is important to understand because you need to realize
  27. that you cannot print out Mon, Tue, etc., but can only use them for
  28. indexing control statements.
  29.  
  30. The second line of the type definition defines Time_Of_Day as
  31. another scalar which can have any of four different values, namely
  32. those listed.  The variable Time can only be assigned one of four
  33. values since it is defined as the type Time_Of_Day.  It should be
  34. clear that even though it can be assigned Morning, it cannot be
  35. assigned Morning_time or any other variant spelling of Morning,
  36. since it is simply another identifier which must have an exact
  37. spelling to be understood by the compiler.  Several real variables
  38. are defined to allow us to demonstrate the use of the scalar
  39. variables.  After writing a header in lines 16 through 20, the real
  40. variables are initialized to some values that are probably not real
  41. life values, but will serve to illustrate the scalar variable.
  42.  
  43.  
  44. A BIG SCALAR VARIABLE LOOP
  45. _________________________________________________________________
  46.  
  47. The remainder of the program is one large loop being controlled by
  48. the variable Day as it goes through all of its values, one at a
  49. time.  Note that the loop could have gone from Tue to Sat or
  50. whatever portion of the range desired.  It does not have to go
  51. through all of the values of Day.  Using Day as the case variable
  52. of a case statement, the name of one of the days of the week is
  53. written out each time we go through the loop.  Another loop
  54. controlled by Time is executed four times, once for each value of
  55. Time.  The two case statements within the inner loop are used to
  56. calculate the total pay rate for each time period and each day. 
  57.  
  58.                                                          Page 8-1
  59.  
  60.                             Chapter 8 - Scalars, Subranges & Sets
  61.  
  62. The data is formatted carefully to make a nice looking table of pay
  63. rates as a function of Time and Day.
  64.  
  65. Take careful notice of the fact that the scalar variables never
  66. entered into the calculations, and they were not printed out.  They
  67. were only used to control the flow of logic.  It was much neater
  68. than trying to remember that Mon is represented by a 0, Tue is
  69. represented by a 1, etc.  In fact, those numbers are used for the
  70. internal representation of the scalars but we can relax and let
  71. Pascal worry about the internal representation of our scalars. 
  72. Compile and run this program and observe the output.
  73.  
  74.  
  75. LET'S LOOK AT SOME SUBRANGES
  76. _________________________________________________________________
  77.  
  78. Examine the program SUBRANGE.PAS for an example  ================
  79. of subranges and some additional instruction on    SUBRANGE.PAS
  80. scalar variables.  It may be expedient to define ================
  81. some variables that only cover a part of the
  82. full range as defined in a scalar type.  Notice
  83. that Days is declared a scalar type as in the last program, and
  84. Work is declared a type with an even more restricted range.  In the
  85. var declaration, Day is once again defined as the days of the week
  86. and can be assigned any of the days by the program.  The variable
  87. Workday, however, is assigned the type Work, and can only be
  88. assigned the days Mon through Fri.  If an attempt is made to assign
  89. Workday the value Sat, a run-time error will be generated.  A
  90. carefully written program will never attempt that, and it would be
  91. an indication that something is wrong with either the program or
  92. the data.  This is one of the advantages of Pascal over older
  93. languages and is a reason for the relatively strong type checking
  94. built into the language.
  95.  
  96. Further examination will reveal that Index is assigned the range
  97. of integers from 1 through 12.  During execution of the program,
  98. if an attempt is made to assign Index any value outside of that
  99. range, a run time error will be generated.  Suppose the variable
  100. Index was intended to refer to your employees, and you have only
  101. 12.  If an attempt was made to refer to employee number 27, or
  102. employee number -8, there is clearly an error somewhere in the data
  103. and you would want to stop running the payroll to fix the problem. 
  104. Pascal would have saved you a lot of grief.
  105.  
  106.  
  107. SOME STATEMENTS WITH ERRORS IN THEM.
  108. _________________________________________________________________
  109.  
  110. In order to have a program that would compile without errors, and
  111. yet show some errors, the section of the program in lines 16
  112. through 27 is not really a part of the program since it is within
  113. a comment area.  This is a trick to remember when you are debugging
  114. a program, a troublesome part can be commented out until you are
  115. ready to include it with the rest of the code.  The errors are self
  116.  
  117.                                                          Page 8-2
  118.  
  119.                             Chapter 8 - Scalars, Subranges & Sets
  120.  
  121. explanatory and it would pay for you to spend enough time to
  122. understand each of the errors.
  123.  
  124. There are seven assignment statements as examples of subrange
  125. variable use in lines 29 through 35.  Notice that the variable Day
  126. can always be assigned the value of either Workday or Weekend, but
  127. the reverse is not true because Day can assume values that would
  128. be illegal to assign to the others.
  129.  
  130.  
  131. THREE VERY USEFUL FUNCTIONS
  132. _________________________________________________________________
  133.  
  134. Lines 37 through 42 of the example program demonstrate the use of
  135. three very important functions when using scalars.  The first is
  136. the Succ function that returns the value of the successor to the
  137. scalar used as an argument, the next value.  If the argument is the
  138. last value, a run time error is generated.  The next function is
  139. the Pred function that returns the predecessor to the argument of
  140. the function.  Finally the Ord function which returns the ordinal
  141. value of the scalar.  
  142.  
  143. All scalars have an internal representation starting at 0 and
  144. increasing by one until the end is reached.  In our example
  145. program, Ord(Day) is 5 if Day has been assigned Sat, but
  146. Ord(Weekend) is 0 if Weekend has been assigned Sat.  As you gain
  147. experience in programming with scalars and subranges, you will
  148. realize the value of these three new functions.
  149.  
  150. A few more thoughts about subranges are in order before we go on
  151. to another topic.  A subrange is always defined by two predefined
  152. constants, and is always defined in an ascending order.  A variable
  153. defined as a subrange type is actually a variable defined with a
  154. restricted range.  Good programming practice would dictate that
  155. subranges should be used as often as possible in order to prevent
  156. garbage data.  There are actually very few variables ever used that
  157. cannot be restricted by some amount.  The limits may give a hint
  158. at what the program is doing and can help in understanding the
  159. program operation.  Subrange types can only be constructed using
  160. the simple types, integer, char, byte, or scalar.
  161.  
  162. Compile and run this program even though it has no output.  Add
  163. some output statements to see what values some of the variables
  164. assume.
  165.  
  166.  
  167. SETS
  168. _________________________________________________________________
  169.  
  170. Now for a new topic, sets.  Examining the          ==============
  171. example Pascal program SETS.PAS will reveal some      SETS.PAS
  172. sets.  A scalar variable is defined first, in      ==============
  173. this case the scalar type named Goodies.  A set
  174. is then defined with the reserved words set of
  175.  
  176.                                                          Page 8-3
  177.  
  178.                             Chapter 8 - Scalars, Subranges & Sets
  179.  
  180. followed by a predefined scalar type.  Several variables are
  181. defined as sets of Treat, after which they can individually be
  182. assigned portions of the entire set.
  183.  
  184. Consider the variable Ice_Cream_Cone which has been defined as a
  185. set of type Treat.  This variable is composed of as many elements
  186. of Goodies as we care to assign to it.  In the program, we define
  187. it as being composed of Ice_Cream, and Cone.  The set
  188. Ice_Cream_Cone is therefore composed of two elements, and it has
  189. no numerical or alphabetic value as most other variables have.  
  190.  
  191. In lines 21 through 26, you will see four more delicious deserts
  192. defined as sets of their components.  Notice that the banana split
  193. is first defined as a range of terms, then another term is added
  194. to the group illustrating how you can add to a set.  All five are
  195. combined in the set named Mixed, then Mixed is subtracted from the
  196. entire set of values to form the set of ingredients that are not
  197. used in any of the deserts.  Each ingredient is then checked to see
  198. if it is in the set of unused ingredients, and printed out if it
  199. is.  Note that in is another reserved word in Pascal.  Running the
  200. program will reveal a list of unused elements.
  201.  
  202. In this example, better programming practice would have dictated
  203. defining a new variable, possibly called Remaining for the
  204. ingredients unused in line 32.  It was desirable to illustrate that
  205. Mixed could be assigned a value based on subtracting itself from
  206. the entire set, so the poor variable name was used.
  207.  
  208. When you compile and run this program you will see that this
  209. example results in some nonsense results but hopefully it led your
  210. thinking toward the fact that sets can be used for inventory
  211. control, possibly a parts allocation scheme, or some other useful
  212. system.
  213.  
  214.  
  215.  
  216. SEARCHING WITH SETS
  217. _________________________________________________________________
  218.  
  219. The Pascal program FINDCHRS.PAS is more useful   ================
  220. than the last one.  In it we start with a short    FINDCHRS.PAS
  221. sentence and search it for all lower case        ================
  222. alphabetic letters and write a list of those
  223. used.  Since we are using a portion of the
  224. complete range of char, we do not need to define a scalar before
  225. defining the set, we can define the set using the range 'a'..'z'. 
  226. The set Data_Set is assigned the value of no elements in the first
  227. statement of the program, and the print string, named Print_Group,
  228. is set to blank in the next.  The variable Storage is assigned the
  229. sentence to search, and the search loop is begun.  Each time
  230. through the loop, one of the characters is checked.  It is either
  231. declared as a non-lower-case character, as a repeat of one already
  232. found, or as a new character to be added to the list.
  233.  
  234.  
  235.                                                          Page 8-4
  236.  
  237.                             Chapter 8 - Scalars, Subranges & Sets
  238.  
  239. You are left to decipher the details of the program, which should
  240. be no problem since there is nothing new here.  Run the program and
  241. observe how the list grows with new letters as the sentence is
  242. scanned.
  243.  
  244.  
  245.  
  246. PROGRAMMING EXERCISE
  247. _________________________________________________________________
  248.  
  249. 1.   Modify FINDCHRS.PAS to search for upper-case letters.
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.                                                          Page 8-5
  295.