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

  1.  
  2.  
  3.  
  4.  
  5.                                                        Chapter 10
  6.                                                            ARRAYS
  7.  
  8.  
  9. OUR FIRST ARRAY
  10. _________________________________________________________________
  11.  
  12. An array is a group of two or more elements that ================
  13. are all of the same type.  In Ada, as in most       ARRAY1.ADA
  14. modern computer languages, arrays can be made of ================
  15. many different kinds of data, but all elements
  16. of an array must be of the same type.  The best
  17. way to see this is to inspect the program named ARRAY1.ADA which
  18. contains a few examples of arrays.
  19.  
  20.  
  21.  
  22. HOW DO WE DECLARE A SUBSCRIPT?
  23. _________________________________________________________________
  24.  
  25. For simplicity, we will start with line 11 where we have a
  26. declaration of the array named Dummy1.  This line says that the
  27. variable named Dummy1 will have 7 elements numbered from 1 through
  28. 7, and each element will have the ability to store one BOOLEAN
  29. variable.  We will see shortly that the individual elements will
  30. be referred to by the variable name followed by a subscript in
  31. parentheses, or Dummy1(1), Dummy1(2),... to Dummy1(7).  Keep in
  32. mind that each is a single BOOLEAN variable.
  33.  
  34. To define an array, we use the reserved words array and of with the
  35. appropriate modifiers as illustrated in this example.  We define
  36. a range which the array will cover, the type of the range variable,
  37. which must be composed of discrete type limits, and the type of
  38. each element of the array.  Remember that a discrete type is any
  39. type of the integer class including enumerated types.  We will have
  40. an example program with an enumerated array index in part 2 of this
  41. tutorial.
  42.  
  43.  
  44.  
  45. LET'S LOOK AT ANOTHER ARRAY DECLARATION
  46. _________________________________________________________________
  47.  
  48. In line 12, we have Dummy2 defined as an array of BOOLEAN type
  49. variables that covers the range of Dummy2(-21) through Dummy2(10),
  50. since N has the value of 10.  Line 13 illustrates declaring the
  51. array Dummy3 as an array of BOOLEAN variables from Dummy3(-21)
  52. through Dummy3(10), but this time the subscript type is not
  53. explicitly stated, only implied by the values of the subscript
  54. limits.  Actually, the subscript type was not needed in the first
  55. two either, since they were implied.  The type INTEGER is used so
  56. often for an array subscript that the type INTEGER has been defined
  57.  
  58.                                                         Page 10-1
  59.  
  60.                                               Chapter 10 - Arrays
  61.  
  62. as the default if none is given.  This is done only to make it a
  63. little simpler to use arrays.
  64.  
  65. So far in this program, we have defined about 70 variables that
  66. have no initial values because we have not assigned them any.  We
  67. will see how to initialize an array later, but first we should
  68. learn how to use them.
  69.  
  70.  
  71.  
  72. HOW DO WE DEFINE AN ARRAY TYPE?
  73. _________________________________________________________________
  74.  
  75. Line 15 gives the general method of declaring an array type.  It
  76. uses the reserved word type followed by the type name, the reserved
  77. word is, then the definition of the type.  The definition is
  78. composed of the range, followed by the reserved word of and the
  79. element type.  We now have a type name which can be used to define
  80. any number of array variables, and each will be made up of integer
  81. variables.  Each will also have 5 elements and will cover the range
  82. of 1 through 5.  Thus line 17 defines an array of five elements
  83. named Total, the elements of which will be named Total(1),
  84. Total(2), ... Total(5).  Each of these elements can be used to
  85. store one data point of type INTEGER.
  86.  
  87. Line 25 of the program illustrates how we can assign a value of 12
  88. to one of the elements of First, which is another array of type
  89. MY_ARRAY and therefore composed of 5 elements.  The second element
  90. is assigned the value of 16, and the third is assigned a value
  91. found by subtracting the first element from the second, resulting
  92. in a value of 4.  This illustrates the use of the elements once
  93. they are assigned values.  The fourth and fifth elements are also
  94. assigned nonsense data, illustrating some mathematical operations.
  95. The assignment in line 28 will be explained in the next paragraph.
  96. If you remember that each element is an INTEGER type variable, you
  97. can use them just like any other INTEGER type variable, except that
  98. you must add the subscript to indicate which element you are
  99. interested in using at each point in the program.
  100.  
  101.  
  102.  
  103. RENAMING AN ARRAY ELEMENT
  104. _________________________________________________________________
  105.  
  106. Line 22 is an illustration of renaming a single element of the
  107. array.  Once again, this simply gives us a synonym which we can use
  108. to refer to the variable, it does not declare another variable.
  109. It should be pointed out that it is not permitted to rename a type
  110. but you can achieve the same effect by declaring a subtype with the
  111. same range as the type which you wish to have another name for.
  112.  
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 10-2
  118.  
  119.                                               Chapter 10 - Arrays
  120.  
  121. WHAT IS A RANGE_ERROR?
  122. _________________________________________________________________
  123.  
  124. Any attempt to use a subscript which is outside of the assigned
  125. range will result in the system raising the exception Range_Error,
  126. and will be handled as a fatal error, terminating operation of the
  127. program unless you handle the exception yourself.  We will study
  128. exceptions in part 2 of this tutorial.
  129.  
  130. The subscript can be a variable itself, provided it is of the
  131. correct type, as is illustrated in line 32, where all five elements
  132. of the array named Second are assigned nonsense data for
  133. illustration.  The subscript can also be calculated, with any
  134. arbitrary level of complexity, provided of course that it results
  135. in an INTEGER type result and is within the range of the declared
  136. subscript.
  137.  
  138.  
  139.  
  140. ARRAY ASSIGNMENT
  141. _________________________________________________________________
  142.  
  143. The assignment in line 35 is legal, provided the two arrays are of
  144. the same type, resulting in all 5 values of the array named First
  145. being assigned to the five elements of the array named Total.  Line
  146. 36 illustrates that arrays can even be compared for equality or
  147. inequality, and they are considered equal if each element of Total
  148. is equal to the corresponding element of First.  If there is any
  149. inequality, the result is FALSE.
  150.  
  151. Lines 41 through 48 illustrate a few more of the permissible
  152. operations on arrays.  You should have no trouble studying them on
  153. your own.
  154.  
  155.  
  156.  
  157. WHAT IS AN ANONYMOUS TYPE?
  158. _________________________________________________________________
  159.  
  160. In line 15, we defined a type and gave it a name which we could
  161. then use at will throughout the remainder of the program.  Any
  162. array of this type is said to be of type MY_ARRAY, because it has
  163. a name assigned to it.  The array declared in line 20 does not have
  164. a name associated with it, so it is referred to as an anonymous
  165. type.  An array is the only place in Ada where it is possible to
  166. have an anonymous type, and its use should be discouraged for
  167. reasons to be seen shortly.
  168.  
  169. The array assignment in line 35 was only possible because the two
  170. arrays were of the same exact type, and in order to be of the same
  171. type, they must be declared with the same type name.  The array
  172. named Funny has the identical structure as the array First, but it
  173. was not declared with the same type name, and it is therefore of
  174. a different type and cannot be used in an array assignment
  175.  
  176.                                                         Page 10-3
  177.  
  178.                                               Chapter 10 - Arrays
  179.  
  180. statement.  Since the array Funny is of an anonymous type, it is
  181. impossible to define another array with the same type, so it is
  182. impossible to use this array in an assignment statement with any
  183. other array.
  184.  
  185. Line 21 declares the arrays X and Y in the same statement, and it
  186. would seem that they should have assignment compatibility, but
  187. according to the definition of Ada, the two arrays are of different
  188. types because naming both variable names in one statement is merely
  189. a shorthand method for naming them in two separate lines.  The two
  190. arrays are therefore each of a separate anonymous type.  This is
  191. a fine point, but should be clearly understood.
  192.  
  193. Two arrays are assignment compatible only if they are declared with
  194. the same type name.  Compile and run this program and compare the
  195. output with the output you expect.
  196.  
  197.  
  198.  
  199. WHAT IS A SLICE OF AN ARRAY?
  200. _________________________________________________________________
  201.  
  202. The program named SLICE.ADA contains several      ===============
  203. examples of the use of the slice in Ada, which       SLICE.ADA
  204. is a portion of an array.  You may wish to        ===============
  205. assign part of an array to part of another array
  206. in a single statement.  This can be done with a
  207. slice.
  208.  
  209. We begin by declaring an array type, MY_ARRAY, which is then used
  210. to declare two arrays, First and Second.  Finally we declare a
  211. third array named Funny, which is of an anonymous type as defined
  212. in the last example program.  This example program will illustrate
  213. the difficulty of working with an array of anonymous type, but we
  214. will start by working with the named arrays.
  215.  
  216.  
  217.  
  218. WHAT IS A SLICE?
  219. _________________________________________________________________
  220.  
  221. In the executable part of the program, we assign nonsense values
  222. to the arrays named Funny and First, so we will have some data to
  223. work with.  Then in line 25 we tell the system to take elements 3
  224. through 7 of the array named First and assign them to elements 1
  225. through 5 of the array named Second.  The term on each side of the
  226. assignment operator is a slice, a portion of an array.  In order
  227. to do the slice assignment as illustrated, both arrays must be of
  228. the same type and both slices must have the same number of
  229. elements.  Of course, all slice limits must be within the declared
  230. range limits of the subscripts for the type in use.  Line 26
  231. illustrates copying 4 elements from First to Second, and line 27
  232. illustrates copying 6 elements.
  233.  
  234.  
  235.                                                         Page 10-4
  236.  
  237.                                               Chapter 10 - Arrays
  238.  
  239. In line 28, eight elements are copied from an array to itself in
  240. such a manner that the destination and origin portions of the array
  241. overlap.  Ada is defined such that all of the values are copied in
  242. true fashion rather than recopying some earlier copied values again
  243. as the copying of values continues.  This is because the entire
  244. right hand expression is evaluated before the assignment is made
  245. to the left side variable.  Note that the slice can only be used
  246. with a singly dimensioned array.
  247.  
  248.  
  249.  
  250. BACK TO THE ANONYMOUS TYPE VARIABLE
  251. _________________________________________________________________
  252.  
  253. We said that the variable named Funny is an anonymous type and that
  254. it would cause some difficulties, so let's see what the problems
  255. are.  In order to assign all or part of an array to another type
  256. of array, we must use a type transformation or get a compile error.
  257. Line 30 illustrates how to use a type transformation, as we have
  258. seen before. However, we can only do a type transformation based
  259. on the entire array, not a portion of it, so we can only transform
  260. the type from anonymous to a full sized array of the target type,
  261. MY_ARRAY.  Therefore we can only copy a slice from the anonymous
  262. type variable into the full array of the target.  There is no way
  263. to transform the type from MY_ARRAY to the anonymous type, since
  264. the anonymous type doesn't have a name, so a slice cannot be used
  265. to assign to the array variable named Funny.  Line 31 illustrates
  266. another slice assignment to the complete array named First.
  267.  
  268. The entire array named First is displayed for your observation.
  269. Compile and run this program and examine the output.
  270.  
  271.  
  272.  
  273. A MULTI DIMENSIONAL ARRAY
  274. _________________________________________________________________
  275.  
  276. Examine the program named MULTARY1.ADA for our   ================
  277. first example of a multidimensional array.  We     MULTARY1.ADA
  278. begin by declaring a type named MATRIX which is  ================
  279. composed of an array of an array with a total of
  280. 12 elements.  Each element of the array is
  281. referred to by two subscripts following the variable name in
  282. parentheses as illustrated in the executable part of the program.
  283. Lines 21 through 26 contain a nested loop to fill the variable
  284. named Square_Board with a multiplication table, and to fill
  285. Chess_Board with all zeros.  Note that the variable named
  286. Chess_Board is of an anonymous type because there is no type name
  287. associated with it.
  288.  
  289. The entire array named Square_Board is assigned to the array
  290. Checker_Board which is legal because they are of the same type,
  291. which means they were defined with the same type name.  Line 30 is
  292. used to assign a value of 2 to one element of the Checker_Board,
  293.  
  294.                                                         Page 10-5
  295.  
  296.                                               Chapter 10 - Arrays
  297.  
  298. and that value is used in line 31, which states,
  299. "Checker_Board(2,4) := 17;", because line 30 assigned the value of
  300. 2 to Checker_Board(2,3).
  301.  
  302. It should be clear to you, based on the discussion of the last
  303. program, that even though Chess_Board has the same structure as
  304. Square_Board, they are not type compatible and are not assignment
  305. compatible.  The individual elements are assignment compatible
  306. however.
  307.  
  308. The array named Checker_Board is displayed which you can observe
  309. when you compile and run this program.
  310.  
  311.  
  312.  
  313. WE NEED SOME FLEXIBILITY
  314. _________________________________________________________________
  315.  
  316. This program used fixed values for all of the    ================
  317. range and loop definitions, which allows very      MULTARY2.ADA
  318. little flexibility, and is therefore considered  ================
  319. to be poor programming practice.  Of course, it
  320. was done for clarity since this was your first
  321. look at a multidimensional array.  The next program, named
  322. MULTARY2.ADA is much more flexible and illustrates some of the
  323. slightly more advanced techniques which can be used with Ada.
  324.  
  325. This program is identical to the last except that there are two
  326. constants defined in the declaration part which are then used to
  327. define the limits of the arrays and the loops.  If you needed to
  328. make the program cover a larger range, it would be trivial to
  329. modify the constants and recompile the program.  Compile and run
  330. this program and you will see that it does exactly the same thing
  331. as the last one.
  332.  
  333.  
  334.  
  335. WE NEED MORE FLEXIBILITY
  336. _________________________________________________________________
  337.  
  338. Examine the program named MULTARY3.ADA and you   ================
  339. will find that it is identical to the last two     MULTARY3.ADA
  340. except in the way we define the loop limits.     ================
  341. Recall the information we covered on attributes
  342. in an earlier lesson and the additions to this
  343. program will be simple for you to understand.  In line 24 we use
  344. the attribute LAST to define the upper limit of the outer loop and
  345. add the digit "1" in parentheses to tell the system that we are
  346. interested in the first subscript of the array named Square_Board.
  347. Line 25 uses a "2" to indicate the LAST value of the second
  348. subscript of Square_Board.  If no subscript indication is given,
  349. the system will default to "1", but it is much clearer for the
  350. reader to indicate the "1" in parentheses. It may seem to you to
  351. be a lot of trouble to define the limits this way, but when we get
  352.  
  353.                                                         Page 10-6
  354.  
  355.                                               Chapter 10 - Arrays
  356.  
  357. to the point where we are writing generalized procedures we will
  358. need the flexibility given here.  Generic procedures are a long way
  359. off too, but these techniques will be absolutely essential when we
  360. study them.
  361.  
  362. Lines 37 and 38 also use attributes for the two loop ranges, but
  363. they use the RANGE attribute with the number of the desired
  364. subscript in parentheses once again.
  365. When using a singly subscripted array, it is legal to use a "1" in
  366. parentheses also, but if none is given, the system will default to
  367. one.  You should explicitly include the number in a
  368. multidimensional array and omit it for a singly dimensioned array
  369. as a matter of programming clarity.
  370.  
  371.  
  372.  
  373. WHAT IS AN AGGREGATE?
  374. _________________________________________________________________
  375.  
  376. An aggregate is a grouping of numeric literals, although
  377. enumeration values could also be included, that is used to
  378. initialize an array in the present case.  The literals can be
  379. grouped in the order in which they are used, and this is referred
  380. to as a positional aggregate.  The literals can also be in a named
  381. aggregate, in which the use for each value is defined by using the
  382. name of the location to which it should be assigned.  The
  383. definition of mixed notation will be deferred until later since it
  384. is not permitted to be used for array initialization anyway.
  385.  
  386.  
  387.  
  388. HOW DO WE INITIALIZE ARRAYS?
  389. _________________________________________________________________
  390.  
  391. Examine the program named ARRYINIT.ADA for some  ================
  392. examples of array initialization.  Seven arrays    ARRYINIT.ADA
  393. are declared and the method of aggregate         ================
  394. notation, both positional and named, are
  395. illustrated.  The variable Total is initialized
  396. using the positional notation and the variable First is initialized
  397. by use of the named notation.  Mixed notation is not allowed for
  398. array initialization.  The array named Another in line 16 contains
  399. a new construct using the reserved word others in conjunction with
  400. the named aggregate notation.  If included, it must be the last
  401. entry in the aggregate.  The values of Another(1), Another(3), and
  402. Another(4) will be initialized to the value of 3.  The array named
  403. One_More in line 18 illustrates initialization of a range of
  404. variables to the value 13, and two variables to the value 27.  Note
  405. that the others case can be included here but must be last and
  406. alone.  The other singly dimensioned arrays should pose no problem
  407. for you but a few comments are in order concerning the multi
  408. dimensional arrays.
  409.  
  410.  
  411.  
  412.                                                         Page 10-7
  413.  
  414.                                               Chapter 10 - Arrays
  415.  
  416. Even though you are not permitted to use mixed aggregate notation
  417. for an array, the rule is applied at only one level so you can use
  418. different methods for each level.  The variable Square_Board uses
  419. all positional notation, but Checker_Board uses a named aggregate
  420. for the first subscript and positional for the second.  Chess_Board
  421. mixes things up a little more by using a named aggregate for the
  422. first subscript and both methods for the second subscript even
  423. though it is consistent within each subgroup and is therefore
  424. obeying the rules.
  425.  
  426.  
  427.  
  428. MORE ARRAY EXAMPLES LATER
  429. _________________________________________________________________
  430.  
  431. There is more to be said about arrays, but it will have to wait
  432. until we cover a few more topics.  This is meant to get you started
  433. using arrays, but in a later chapter we will cover additional array
  434. topics.
  435.  
  436.  
  437.  
  438. PROGRAMMING EXERCISES
  439. _________________________________________________________________
  440.  
  441. 1.   Modify ARRAY1.ADA in such a way that the variables X and Y are
  442.      assignment compatible.
  443.  
  444. 2.   Write a program with two arrays of size 3 by 5 each and
  445.      initialize each to a suitable set of integer values.  Multiply
  446.      these, element by element, and store the values in a third
  447.      array.  Finally, display the results in a clear format on the
  448.      monitor.
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.                                                         Page 10-8