home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / m2t-1.zip / CHAP10.TXT < prev    next >
Text File  |  1989-01-18  |  15KB  |  351 lines

  1.  
  2.                                                     Chapter 10
  3.  
  4.                                     SCALARS, SUBRANGES, & SETS
  5.  
  6.  
  7.  
  8. PREREQUISITES FOR THIS MATERIAL
  9. ______________________________________________________________
  10.  
  11. In order to understand the material in this chapter, you
  12. should have a fairly good understanding of most of the
  13. material in Part I of this tutorial.
  14.  
  15. A scalar, also called an enumerated type,      ===============
  16. is a list of values which a variable of          ENTYPES.MOD
  17. that type may assume.  Examine the file        ===============
  18. named ENTYPES.MOD for an example of some
  19. scalars.  The first TYPE declaration defines Days as being a
  20. type which, if used to define a variable, the variable can be
  21. assigned any one of the seven values listed.  Since, within
  22. the VAR declaration, Day is assigned the type of Days, then
  23. Day is a variable which can assume any one of seven different
  24. values.  Moreover, Day can be assigned the value mon, or tue,
  25. etc., which makes the program easier to follow and understand.
  26. Internally, the Modula-2 system does not actually assign the
  27. value mon to the variable Day, but it uses an integer
  28. representation for each of the names.  This is important to
  29. understand because you must realize that you cannot print out
  30. mon, tue, etc., but can only use them for indexing control
  31. statements.
  32.  
  33. Note that there is an upper limit of 16 values for an
  34. enumerated type placed on you by most implementations of
  35. Modula-2.  This is actually a very low limit and it is most
  36. unfortunate that such a small limit exists.  You should check
  37. your documentation because your particular compiler may allow
  38. more.
  39.  
  40.  
  41. ANOTHER ENUMERATED TYPE
  42. ______________________________________________________________
  43.  
  44. The second line of the type definition defines TimeOfDay as
  45. another enumerated type.  The variable Time can only be
  46. assigned one of four values since it is defined as the type
  47. TimeOfDay.  It should be clear that even though it can be
  48. assigned morning, it cannot be assigned morningtime or any
  49. other variant spelling of morning, since each enumerated value
  50. is simply another identifier which must have an exact spelling
  51. to be understood by the compiler.
  52. Several real variables are defined to allow us to demonstrate
  53. the use of the enumerated variables.  After writing a header
  54. for neat formatting of the output, the real variables are
  55. initialized to some values that are probably not real life
  56. values, but will serve as a platform to illustrate use of the
  57. enumerated variable.
  58.  
  59.                                                           10-1
  60.  
  61.                       Chapter 10 - Scalars, Subranges and Sets
  62.  
  63. A BIG SCALAR VARIABLE LOOP
  64. ______________________________________________________________
  65.  
  66. The remainder of the program is one large loop being
  67. controlled by the variable Day as it goes through all of its
  68. values, one at a time.  Note that the loop could have gone
  69. from tue to sat or any portion of the range desired.  It does
  70. not have to go through all of the values of Day.  Using Day
  71. as the case variable, the name of one of the days of the week
  72. is written out each time we go through the loop.  Another loop
  73. controlled by Time is executed four times, once for each value
  74. of Time.  The two CASE statements within the inner loop are
  75. used to calculate the total pay rate for each time period and
  76. each day.  The data is formatted carefully to make a nice
  77. looking table of pay rates as a function of Time and Day.
  78.  
  79. Take careful notice of the fact that the scalar variables
  80. never entered into the calculations, and they were not printed
  81. out.  They were only used to control the flow of the logic.
  82. It was much neater than trying to remember that mon is
  83. represented by a 0, tue is represented by a 1, etc.  In fact,
  84. those numbers are used for the internal representation of the
  85. scalars, but we can relax and let the Modula-2 system worry
  86. about the internal representation of our scalars.
  87.  
  88.  
  89.  
  90. THE OUTPUT MAY NOT BE VERY NEAT
  91. ______________________________________________________________
  92.  
  93. Compile and run this program and observe the form of the
  94. output data.  The only format available with some compilers
  95. is the E (exponential) notation which does not make for a very
  96. well formatted or easily read output.  Don't let this worry
  97. you, when we get to Part III of this tutorial we will see how
  98. we can write our own output routines to display or print
  99. floating point numbers in any format we can think up.
  100.  
  101. One other thing should be pointed out in this module.  If you
  102. observe the case statements you will notice that the one that
  103. starts in line 33 does not have an else clause.  It is really
  104. not needed because every possible value of the variable Day
  105. is covered in the various branches.  In the case statement
  106. starting in line 51, there is an else clause, because only two
  107. of the possible 7 values are acted on in the case body itself.
  108. Without the else, the program would not know what to do with
  109. a value of mon through fri, so the else is required here, but
  110. not in the earlier one.
  111.  
  112.  
  113.  
  114.  
  115.  
  116.                                                           10-2
  117.  
  118.                      Chapter 10 - Scalars, Subranges, and Sets
  119.  
  120. A COMMENT ON PROGRAM STYLE
  121. ______________________________________________________________
  122.  
  123. The enumerations in this program used a named type, but an
  124. anonymous type can also be used.  Good programming practice
  125. would be to use a named type however.
  126.  
  127. The case statement in lines 51 through 55 could easily be
  128. replaced by an if statement.  The case statement was selected
  129. here simply because the other constructs use the case
  130. statement and it was felt that using the same construct would
  131. lead to a clearer program.
  132.  
  133.  
  134. LETS LOOK AT SOME SUBRANGES
  135. ______________________________________________________________
  136.  
  137. Examine the program SUBRANGE.MOD for an       ================
  138. example of subranges.  It may be                SUBRANGE.MOD
  139. expedient, for a particular programming       ================
  140. problem, to define some variables that
  141. only cover a part of the full range as defined in a scalar
  142. type.  Notice that Days is declared a scalar type as in the
  143. last program, and Work is declared a type with an even more
  144. restricted range, a subrange of Days.  The variable named Rest
  145. is declared as a different subrange of Days.  In the var
  146. declaration, Day is once again defined as the days of the week
  147. and can be assigned any of the days by the program.  The
  148. variable Workday, however, is assigned the type Work, and can
  149. only be assigned the days mon through fri because of the more
  150. limited range of the type.  If an attempt is made to assign
  151. Workday the value sat, a runtime error will be generated
  152. because sat is not a legal value to be assigned to Workday.
  153. A carefully written program will never attempt to do so, and
  154. it would therefore be an indication that something is wrong
  155. with either the program or the data.  This is one of the
  156. advantages of Modula-2 over older less structured languages.
  157.  
  158. Further examination will reveal that Index is declared as
  159. being capable of storing only the range of values from 1 to
  160. 12, another subrange.  During execution of the program, if an
  161. attempt is made to assign Index any value outside of that
  162. range, a runtime error will be generated.  Suppose the
  163. variable named Index was intended to refer to your employees,
  164. and you have only 12.  If an attempt was made to refer to
  165. employee number 27, or employee number -8, there is clearly
  166. an error somewhere in the data and you would want to stop
  167. running the payroll to fix the problem.  Modula-2 would have
  168. saved you a lot of grief.
  169.  
  170. The way Index is defined, it will be of type cardinal because
  171. the entire range is composed of positive numbers.  It is
  172. therefore not possible to use the variable Index in
  173. calculations involving integer type numbers.  If the range
  174.  
  175.                                                           10-3
  176.  
  177.                      Chapter 10 - Scalars, Subranges, and Sets
  178.  
  179. were defined with at least one end negative, it would be of
  180. type integer.  Index2 will be of type integer because we force
  181. the compiler to use the integer type through use of the type
  182. written prior to the range.  Modula-2 gives you complete
  183. control over the types used.
  184.  
  185.  
  186. SOME STATEMENTS WITH ERRORS IN THEM
  187. ______________________________________________________________
  188.  
  189. In order to have a program that would compile without errors,
  190. and yet include some examples of errors, the first part of the
  191. program is not really a part of the program since it is within
  192. a comment area.  This is a trick to remember when you are
  193. debugging a program, a troublesome part can be commented out
  194. until you are ready to include it with the rest.  The errors
  195. should be self explanatory.
  196.  
  197. Going beyond the area commented out, there are seven
  198. assignment statements in lines 31 through 37 given as examples
  199. of subrange variable use.  Notice that the variable Day can
  200. always be assigned the value of either Workday or Weekend, but
  201. the reverse is not true because Day can store values that
  202. would be illegal for the other variables.
  203.  
  204.  
  205. THREE VERY USEFUL FUNCTIONS
  206. ______________________________________________________________
  207.  
  208. Lines 39 through 45 of the example program demonstrate the use
  209. of three very important functions when using scalars.  The
  210. first is the INC function which returns the value of the
  211. scalar following the value of the scalar used as the actual
  212. parameter.  If the parameter is the last value in the list,
  213. a runtime error is generated.  The next function is the DEC
  214. that returns the value of the scalar prior to the one used in
  215. the actual parameter.  All scalars have an internal
  216. representation starting at 0 and increasing by one until the
  217. end is reached.  The third function is the ORD which simply
  218. returns the numerical value of the scalar variable.
  219.  
  220. In our example program, ORD(Day) is 5 if Day has been assigned
  221. sat, but ORD(Weekend) is 0 if Weekend has been assigned sat.
  222. As you gain experience in programming with scalars and
  223. subranges, you will realize the value of these three
  224. functions.
  225.  
  226. A few more thoughts about subranges are in order before we go
  227. on to another topic.  A subrange is always defined by two
  228. predefined constants, and is always defined in an ascending
  229. order.  A variable defined as a subrange type is actually a
  230. variable defined with a restricted range, and should be used
  231. as often as possible in order to prevent garbage data.  There
  232. are actually very few variables ever used in any computer
  233.  
  234.                                                           10-4
  235.  
  236.                      Chapter 10 - Scalars, Subranges, and Sets
  237.  
  238. program that cannot be restricted by some amount.  The limits
  239. may give a hint at what the program is doing and can therefore
  240. help in understanding the program operation.
  241.  
  242. Subrange types can only be constructed using the simple data
  243. types, not including the real type, and any operation that can
  244. be performed on a particular type can be performed on a
  245. subrange of that type.
  246.  
  247.  
  248. SETS
  249. ______________________________________________________________
  250.  
  251. Now for a new topic, sets.  Examining the     ================
  252. example program SETS.MOD will reveal the          SETS.MOD
  253. use of some sets.  A scalar type is           ================
  254. defined first, in this case the scalar
  255. type named Goodies.  A set type is then defined with the
  256. reserved words SET OF followed by a predefined scalar type.
  257. Most microcomputers have an upper limit of 16 elements that
  258. can be used in a set.  Your compiler documentation will tell
  259. you if yours allows a larger range.
  260.  
  261. Several variables are defined as sets of Treat, after which
  262. they can individually be assigned portions of the entire set.
  263. Consider the variable IceCreamCone which has been defined as
  264. a set of type Treat.  This variable is composed of as many
  265. elements of Goodies as we care to assign to it.  In the
  266. program, we define it as being composed of IceCream, and Cone.
  267. The set IceCreamCone is therefore composed of two elements,
  268. and it has no numerical or alphabetic value as most other
  269. variables have.  Continuing in the program, you will see 4
  270. more delicious deserts defined as sets of their components.
  271. Notice that the banana split is first defined as a range of
  272. terms, then another term is added to the group.  All five of
  273. the defined sweets are combined in the set named Mixed, then
  274. Mixed is subtracted from the entire set of values to form the
  275. set of ingredients that are not used in any of the deserts.
  276. Each ingredient is then checked to see if it is IN the set of
  277. unused ingredients, and printed out if it is.  Running the
  278. program will reveal a list of the unused elements.
  279.  
  280. In this example, better programming practice would have
  281. dictated defining a new variable, possibly called Remaining
  282. for the ingredients that were unused.  It was desirable to
  283. illustrate that Mixed could be assigned a value based on
  284. subtracting itself from the entire set, so the poor variable
  285. name was used.
  286.  
  287. This example resulted in some nonsense results but hopefully
  288. it led your thinking toward the fact that sets can be used for
  289. inventory control, possibly a parts allocation scheme, or some
  290. other useful system.
  291.  
  292.                                                           10-5
  293.  
  294.                      Chapter 10 - Scalars, Subranges, and Sets
  295.  
  296. THE BITSET TYPE
  297. ______________________________________________________________
  298.  
  299. The only predefined set type is the BITSET.  This is a set of
  300. bits contained in one word of the host computer, and is
  301. typically composed of 16 bits or elements.  The type BITSET
  302. acts as if it were defined as;
  303.  
  304.      TYPE BITSET = SET OF [0..15];
  305.  
  306. Its use will be left to the students study.  There is an
  307. example of use in chapter 16 of this tutorial.  It will be
  308. found in the program named BITOPS.MOD and it would be a
  309. profitable study to examine this program.
  310.  
  311.  
  312. SET OPERATIONS
  313. ______________________________________________________________
  314.  
  315. A complete list of all operations available with sets is as
  316. follows;
  317.  
  318.      +     Union
  319.      -     Difference
  320.      *     Intersection
  321.      /     Symmetric difference
  322.  
  323.      =     Test for equality
  324.      #     Test for inequality (Modern method)
  325.      <>    Test for inequality (Becoming obsolete)
  326.      <=    Inclusion
  327.      >=    Inclusion
  328.  
  329. A complete discussion of set theory is beyond the scope of
  330. this tutorial.  The interested student will be referred to
  331. academia and popular press.
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.                                                           10-6
  350.  
  351.