home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog3 / chap19.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  12.2 KB  |  292 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                        Chapter 19
  6.                                             ADVANCED ARRAY TOPICS
  7.  
  8.  
  9.  
  10. Examine the file named SUMMER.ADA for an example ================
  11. of an unconstrained array type illustrated in       SUMMER.ADA
  12. line 10.  The box (<>), as it is called, refers  ================
  13. to something that must be filled in later, and
  14. is a construct that will be used many other
  15. places in Ada.  Note that the index type in line 10 is POSITIVE,
  16. a fact that will have a bearing on its use later.
  17.  
  18.  
  19.  
  20. USE OF THE UNCONSTRAINED ARRAY TYPE
  21. _________________________________________________________________
  22.  
  23. In line 12, we declare an array constant of type MY_ARRAY and we
  24. assign values to the constants by use of the positional aggregate.
  25. Since we do not give the range of the indices of the array, the
  26. system will assign them, and in this case will use a range of 1
  27. through 4, because of the use of the type POSITIVE for the index
  28. type.  If we would have used type INTEGER for the index, the
  29. selected range would have been -32768 through -32765, which is the
  30. lower end of the INTEGER range, unless your implementation used a
  31. different lower limit for INTEGER.  Careful selection of the array
  32. index type is important.  In line 13, we declare another constant
  33. array, but this time we use a named aggregate and the system does
  34. not get the opportunity to pick the range, we pick it explicitly
  35. ourselves.  The indices do not have to be named in consecutive
  36. order as is done here, but all values must be given.
  37.  
  38. We declare an uninitialized variable array in line 16, which covers
  39. the range of 1 through 12, and an initialized variable array in
  40. line 17, which uses the positional aggregate method of
  41. initialization.
  42.  
  43.  
  44.  
  45. A VERY FLEXIBLE FUNCTION
  46. _________________________________________________________________
  47.  
  48. The function in lines 20 through 30 appears to use an unconstrained
  49. array for its formal parameter variable, and it does, but with each
  50. use of the function, the formal parameter array is constrained to
  51. the limits of the actual variable.  When the function is called in
  52. line 36 of the program with My_List as the actual array variable,
  53. the range of My_List, 1 through 12, is used for the range of the
  54. formal parameter in the function.  This makes it possible to use
  55. the array attributes within the function, in the manner shown, such
  56. that they are dependent on which array is used for the actual
  57.  
  58.                                                         Page 19-1
  59.  
  60.                                Chapter 19 - Advanced Array Topics
  61.  
  62. parameter.  When Stuff is used for the actual parameter, the formal
  63. parameter will have a range of 4 through 8 within the function.
  64. The function can therefore be written in such a way that it has
  65. flexibility built into it, even though strong type checking
  66. continues to be done, since the function cannot be used with a
  67. different unconstrained type if we had one in this example program.
  68. Note that all array attributes are available here as they are with
  69. any array.
  70.  
  71.  
  72.  
  73. A FEW NOTES ABOUT THIS TYPE
  74. _________________________________________________________________
  75.  
  76. All elements of every variable of type MY_ARRAY will be of type
  77. INTEGER, and all indices will be of subtype POSITIVE including the
  78. range limits.  The variables named My_List and Stuff are of the
  79. same type with different limits, so the slice can be used with
  80. them.  After you study this program, compile and execute it so you
  81. can observe the output.
  82.  
  83.  
  84.  
  85. AN ARRAY WITH AN ENUMERATED INDEX
  86. _________________________________________________________________
  87.  
  88. Examine the program named ENUMARY.ADA for an      ===============
  89. example of an array with an enumerated variable     ENUMARY.ADA
  90. for an index.  Since the index of an array may    ===============
  91. be any discrete type, and an enumerated type is
  92. discrete, it can be used for an array index
  93. according to the Ada standard.
  94.  
  95. We define an enumerated type named DAY, that includes the days of
  96. the week as elements, then declare an array using the type DAY for
  97. the index.  It should be apparent to you that this is an anonymous
  98. array type, the use of which is discouraged in a significant
  99. program, but should cause no type conversion problems in a simple
  100. program such as this one.  Finally, we declare two other simple
  101. variables for later use and begin the executable part of the
  102. program.
  103.  
  104. We use a loop to assign 8.0 hours to each day from MON through FRI,
  105. then assign 4.0 hours to SAT, and 0.0 hours to SUN.  Finally we sum
  106. up the hours for the week and display them on the monitor.  It is
  107. a very simple program, but it illustrates a very useful construct
  108. in Ada.  For that reason, you should spend enough time studying it
  109. until you thoroughly understand it.  Be sure to compile and execute
  110. ENUMARY.ADA.
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 19-2
  118.  
  119.                                Chapter 19 - Advanced Array Topics
  120.  
  121. ARRAY OPERATORS
  122. _________________________________________________________________
  123.  
  124. Some of the operators discussed earlier in this  ================
  125. tutorial are available for use with arrays.        ARRAYOPS.ADA
  126. They operate on each element of the array as if  ================
  127. the operation were done in a loop.  The example
  128. program ARRAYOPS.ADA will illustrate the use of
  129. a few of these operations.
  130.  
  131. Lines 22 through 27 illustrate the logical operators being used
  132. with entire arrays.  The arrays are compared element by element,
  133. and as soon as a difference is found in two corresponding elements,
  134. the comparison result of these two elements is returned as the
  135. overall comparison result.  If all corresponding elements are
  136. equal, the result of the comparison is equal.
  137.  
  138. Note that it is legal and sometimes very useful to use an array of
  139. records.  Arrays of records can be compared for equality or
  140. inequality, but not for the other four operators ( >, >=, <, <= )
  141. because they are illegal for use with individual record elements.
  142. Some thought on your part would have led to this conclusion without
  143. us explicitly stating it.  Many such combinations of operations are
  144. possible with Ada.  It will be up to you to try a few yourself, as
  145. you need them, because there are too many permutations for us to
  146. delineate all of them.
  147.  
  148.  
  149.  
  150. ARITHMETIC ARRAY OPERATORS
  151. _________________________________________________________________
  152.  
  153. The operators illustrated in comments in lines 29 through 34 are
  154. not available as a part of Ada, but they can be made available to
  155. your programs by use of an extension to the language.  The method
  156. will be illustrated in the next example program.
  157.  
  158.  
  159.  
  160. BOOLEAN ARRAY OPERATORS
  161. _________________________________________________________________
  162.  
  163. Two BOOLEAN arrays are assigned some values using positional
  164. aggregates in lines 36 and 37, then used in lines 39 through 46 as
  165. complete arrays.  The logical operators are used in much the same
  166. way that the comparison operators were used previously in this
  167. file.  Note that these operators result in another array, each
  168. element being the result of the logical operation of the
  169. corresponding elements in the compared arrays.  The comparison
  170. operators are available with boolean arrays in a manner similar to
  171. that with other scalar arrays, only a single BOOLEAN type result
  172. value is returned.  Compile and run this program after you
  173. understand the new material presented.
  174.  
  175.  
  176.                                                         Page 19-3
  177.  
  178.                                Chapter 19 - Advanced Array Topics
  179.  
  180. HOW TO WRITE THE ARITHMETIC ARRAY OPERATORS
  181. _________________________________________________________________
  182.  
  183. The example program named ARRAYOP2.ADA           ================
  184. illustrates how to write the arithmetic array      ARRAYOP2.ADA
  185. operators.  This is actually an overloading of   ================
  186. the usual arithmetic operators.  Overloading
  187. these operators is nothing new, you have been
  188. doing it all through this tutorial, because the plus sign, for
  189. example, has been available for adding integer types, as well as
  190. fixed and floating point types.  There is no reason the plus sign
  191. could not be used to add arrays, element by element, and that is
  192. exactly what we will illustrate here.
  193.  
  194. The "+" function is listed in lines 14 through 21 and is a very
  195. simple function, with no difficult code.  The name of the function
  196. is the unusual thing about this function, its name being "+".  This
  197. is the Ada method of overloading operators, and after the function
  198. is defined it is called by using an infix notation, as illustrated
  199. in line 37 of the program where all 6 elements of Group1 are added
  200. to the corresponding elements of Group2, and the 6 results being
  201. assigned to the 6 elements of Crowd.  The results of the addition
  202. are displayed in lines 38 through 43 to show that all elements were
  203. summed.
  204.  
  205. In a similar manner, the function named "mod" is used to provide
  206. an infix notation for the modulo operator, and is called in line
  207. 48.  The other four arithmetic operators are not defined here since
  208. they are so similar to the two which are illustrated.  The
  209. overloading of the mod operator in this way should indicate to you
  210. why it is important to think of mod as an operator rather than a
  211. subprogram call.
  212.  
  213.  
  214.  
  215. OVERLOADING RULES
  216. _________________________________________________________________
  217.  
  218. There are two rules which must be considered when overloading
  219. operators, the first being that you are permitted as many
  220. overloadings for a given operator as you desire, provided that the
  221. parameters of the inputs and result have unique types for each of
  222. the overloadings.  This is because the system uses the types of the
  223. parameters and the type of the result to determine which
  224. overloading you wish to use each time you use the operator.  This
  225. implies, correctly, that you cannot redefine the use of an existing
  226. operator for a given type.
  227.  
  228. The second rule is simple.  You can overload existing Ada
  229. operators, but you cannot define an infix operator that is not
  230. predefined in Ada.  Be sure to compile and execute this program.
  231.  
  232.  
  233.  
  234.  
  235.                                                         Page 19-4
  236.  
  237.                                Chapter 19 - Advanced Array Topics
  238.  
  239. UNARY OPERATOR OVERLOADING
  240. _________________________________________________________________
  241.  
  242. Examine the program named UNARYOP.ADA for an      ===============
  243. example of overloading the unary operators "+"      UNARYOP.ADA
  244. and "-".  The function in lines 32 through 35     ===============
  245. overloads the "+" operator for the array defined
  246. and is of little interest since nothing is
  247. really accomplished here.  The function in lines 37 through 44
  248. overloads the "-" operator and negates each element of the array.
  249. It may seem silly to even bother with overloading the "+" operator,
  250. but it is illustrated here and can be used to advantage when we
  251. come to a study of generic packages.  It may be necessary to allow
  252. use of such a seemingly silly construct in order to write a general
  253. purpose generic package.
  254.  
  255. Be sure to compile and run this program and see that the
  256. illustrated overloadings work as we have stated.
  257.  
  258.  
  259.  
  260. OPERATOR HIDING
  261. _________________________________________________________________
  262.  
  263. In all of the overloadings we have examined, we were careful to use
  264. overloadings that introduced new type combinations so there was
  265. never an instance of hiding another subprogram.  If the type
  266. combinations are already overloaded in a global manner when a
  267. further overloading is declared at a lower level of nesting, the
  268. subprogram called by the global combination will be hidden and the
  269. nested subprogram will be called each time.  Even though care must
  270. be exercised when overloading operators or identifiers, do not fear
  271. use of this powerful technique made available to you by the
  272. designers of Ada.
  273.  
  274.  
  275.  
  276. PROGRAMMING EXERCISES
  277. _________________________________________________________________
  278.  
  279. 1.   Modify the program named ENUMARY.ADA to output the name of
  280.      each day as an enumerated output along with the number of
  281.      hours worked each day.
  282.  
  283. 2.   Write functions for any two of the remaining four arithmetic
  284.      operators in the example program named ARRAYOP2.ADA, and test
  285.      the two new functions.
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.                                                         Page 19-5