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

  1.  
  2.  
  3.  
  4.                                                         Chapter 6
  5.                                                    ARRAYS, TYPES,
  6.                                             CONSTANTS, AND LABELS
  7.  
  8. ARRAYS
  9. _________________________________________________________________
  10.  
  11. At the beginning of this tutorial we said that a computer program
  12. is composed of data and executable statements to do something with
  13. that data.  Having covered nearly all of the programming
  14. statements, we must now go back and fill in some gaps in our data
  15. definition and look at the array in particular.
  16.  
  17. One of the most useful Pascal data structures is   ==============
  18. the array, which is, in the simplest terms, a        ARRAYS.PAS
  19. group of 2 or more identical terms, all having     ==============
  20. the same type.  Let's go directly to an example
  21. to see what an array looks like. Display the
  22. Pascal program ARRAYS.PAS and notice line 5 starting with the word
  23. Automobiles.  The variable Automobiles is defined as an integer
  24. variable but in addition, it is defined to have twelve different
  25. integer variables, namely Automobile[1], Automobile[2],
  26. Automobile[3], .. Automobile[12].
  27.  
  28. The square braces are used in Pascal to denote a subscript for an
  29. array variable.  The array definition given in line 5 is the
  30. standard definition for an array, namely a variable name, followed
  31. by a colon and the reserved word array, with the range of the array
  32. given in square brackets followed by another reserved word of and
  33. finally the type of variable for each element of the array.
  34.  
  35.  
  36.  
  37. USING THE ARRAY
  38. _________________________________________________________________
  39.  
  40. In using the elements of the array in a program, each of the
  41. elements of the array are required to be used in exactly the same
  42. manner as any simple variable having the same type.  Each time one
  43. of the variables is used, it must have the subscript since the
  44. subscript is now part of the variable name.  The subscript
  45. moreover, must be of the type used in the definition and it must
  46. be within the range defined or it will be construed as an error.
  47.  
  48. Now consider the program itself.  As Index is varied from 1 to 12,
  49. the range of the subscripts of the variable Automobile, the 12
  50. variables are set to the series of values 11 to 22.  Any integer
  51. values could be used, this was only a convenient way to set the
  52. values to some well defined numbers.  With the values stored, a
  53. header is now printed and the list of values contained in the array
  54. is printed.  Note carefully that, although the subscripts are
  55. limited to 1 through 12, the values stored in each of the 12
  56. variables are limited only by the range of integers, namely -32768
  57.  
  58.                                                          Page 6-1
  59.  
  60.                     Chapter 6 - Arrays, Types, Constants & Labels
  61.  
  62. to 32767.  Review this material and this program as long as needed
  63. to fully understand it, as it is very important.
  64.  
  65. Keep in mind that the array is actually composed of 12 different
  66. integer type variables that can be used in any way that it is legal
  67. to use any other integer type variable.  Compile and run this
  68. program.
  69.  
  70.  
  71. DOUBLY INDEXED ARRAYS
  72. _________________________________________________________________
  73.  
  74. After understanding the above example program,    ===============
  75. load the program ARRAYS2.PAS to see the next        ARRAYS2.PAS
  76. level of complexity of arrays.  You will see      ===============
  77. that Checkerboard is defined as an array from 1
  78. to 8, but instead of it being a simple data
  79. type, it is itself another array from 1 to 8 of type integer.  The
  80. variable Checkerboard is actually composed of 8 elements, each of
  81. which is 8 elements, leading to a total of 64 elements, each of
  82. which is a simple integer variable.  This is called a doubly
  83. subscripted array and it can be envisioned in exactly the same
  84. manner as a real checker board, an 8 by 8 matrix.  Another way to
  85. achieve the same end is to define the double array as in the next
  86. line of the program where Value is defined as a total of 64
  87. elements.
  88.  
  89. To use either of the two variables in a program, we must add two
  90. subscripts to the variable name to tell the program which element
  91. of the 64 we desire to use.  Examining the program will reveal two
  92. loops, one nested within the other, and both ranging in value from
  93. 1 to 8.  The two loop indices can therefore be used as subscripts
  94. of the defined array variables.  The variable Checkerboard is
  95. subscripted by both of the loop indices and each of the 64
  96. variables is assigned a value as a function of the indices.  The
  97. assigned value has no real meaning other than to illustrate to you
  98. how it is done.  Since the value of Checkerboard is now available,
  99. it is used to define some values to be used for the variable Value
  100. in line 12 of the program.
  101.  
  102. After defining all of those variables, and you should understand
  103. that we have defined a total of 128 variables in the double loop,
  104. 64 of Checkerboard and 64 of Value, they can be printed out.  The
  105. next section of the program does just that, by using another doubly
  106. nested loop, with a Write statement in the center.  Each time we
  107. go through the center of the loop we tell it to print out one of
  108. the 64 variables in the Checkerboard matrix with the indices Index
  109. and Count defining which of the variables to write each time. 
  110. Careful study of the loop should reveal its exact operation.
  111.  
  112. After printing out the matrix defined by the variable Checkerboard
  113. we still have the matrix defined by the variable Value intact (In
  114. fact, we still have all of Checkerboard available because we
  115. haven't changed any of it).  Before printing out the matrix defined
  116.  
  117.                                                          Page 6-2
  118.  
  119.                     Chapter 6 - Arrays, Types, Constants & Labels
  120.  
  121. by Value, let's change a few of the elements just to see how it is
  122. done.  The code in lines 24 to 26 simply change three of the
  123. variables to illustrate that you can operate on all of the matrix
  124. in loops, or on any part of the matrix in simple assignment
  125. statements.  Notice especially line 26, in which Value[3,6] (which
  126. was just set to the value of 3), is used as a subscript.  This is
  127. perfectly legal since it is defined as a simple integer variable
  128. and is within the range of 1 to 8, which is the requirement for a
  129. subscript of the variable Value.  The last part of the program
  130. simply prints out the 64 values of the variable Value  in the same
  131. manner as above.  Notice that when you run the program, the three
  132. values are in fact changed as expected.
  133.  
  134.  
  135. ARRAYS ARE FLEXIBLE
  136. _________________________________________________________________
  137.  
  138. A few more words about arrays before we go on.  The arrays in the
  139. last program were both defined to be square, namely 8 by 8, but
  140. that choice was purely arbitrary.  The subscripts were chosen to
  141. go from 1 to 8 but they could have been chosen to go from 101 to
  142. 108 or any other range needed to clearly define the problem at
  143. hand.  And, as you may have guessed, you are not limited to a
  144. doubly subscripted matrix but you can define a variable with as
  145. many subscripts as you need to achieve your desired end.  There is
  146. a practical limit to the number of subscripts because you can very
  147. quickly use up all of your available memory with one large
  148. subscripted variable.
  149.  
  150.  
  151. THE TYPE DEFINITION
  152. _________________________________________________________________
  153.  
  154. Now that you understand arrays, let's look at a   ===============
  155. more convenient way to define them by examining      TYPES.PAS
  156. the Pascal file TYPES.PAS.  You will notice a     ===============
  157. new section at the beginning of the listing
  158. which begins with the word type.  The word type
  159. is another reserved word which is used at the beginning of a
  160. section to define "user-defined types".  Beginning with the simple
  161. predefined types we studied earlier, we can build up as many new
  162. types as we need and they can be as complex as we desire.  The six
  163. names (from Array_Def to Boat) in the type section are not
  164. variables, but are defined to be types and can be used in the same
  165. manner as we use integer, byte, real, etc.
  166.  
  167.  
  168. PASCAL CHECKS TYPES VERY CAREFULLY
  169. _________________________________________________________________
  170.  
  171. This is a very difficult concept, but a very important one.  The
  172. Pascal compiler is very picky about the types you use for variables
  173. in the program, doing lots of checking to insure that you don't use
  174. the wrong type anywhere in the program.  Because it is picky, you
  175.  
  176.                                                          Page 6-3
  177.  
  178.                     Chapter 6 - Arrays, Types, Constants & Labels
  179.  
  180. could do very little without the ability to define new types when
  181. needed, and that is the reason Pascal gives you the ability to
  182. define new types to solve a particular problem.
  183.  
  184. Some of these types are used in the var declaration part of the
  185. program.  Notice that since Airplane is an array of Dog_Food and
  186. Dog_Food is in turn an array of boolean, then Airplane defines a
  187. doubly subscripted array, each element being a boolean variable. 
  188. This does not define any variables, only a user defined type, which
  189. can be used in a var to define a matrix of boolean variables.  This
  190. is in fact done in the definition of Puppies, which is an array
  191. composed of 72 (6 times 12) boolean variables.  In the same manner,
  192. Stuff is composed of an array of 14 variables, each being an
  193. integer variable.  The elements of the array are, Stuff[12],
  194. Stuff[13], .. Stuff[25].  Notice also that Stuff2 is also defined
  195. in exactly the same manner and is also composed of 14 variables.
  196.  
  197. Careful inspection will reveal that Kitties is a variable which has
  198. the same definition as Puppies.  It would probably be poor
  199. programming practice to define them in different manners unless
  200. they were in fact totally disassociated.  In this example program,
  201. it serves to illustrate some of the ways user-defined types can be
  202. defined.  Be sure to compile and run this program.
  203.  
  204.  
  205. IS THE CONCEPT OF "TYPES" IMPORTANT?
  206. _________________________________________________________________
  207.  
  208. If you spend the time to carefully select the types for the
  209. variables used in the program, the Pascal compiler will do some
  210. debugging for you since it is picky about the use of variables with
  211. different types.  Any aid you can use to help find and remove
  212. errors from your program is useful and you should learn to take
  213. advantage of type checking.  The type checking in Pascal is
  214. relatively weak compared to some other languages such as Modula-2
  215. or Ada, but still very useful.
  216.  
  217. In a tiny program like this example, the value of the type
  218. declaration part cannot be appreciated, but in a large program with
  219. many variables, the type declaration can be used to great
  220. advantage.  This will be illustrated later.
  221.  
  222.  
  223. THE CONSTANT DECLARATION
  224. _________________________________________________________________
  225.  
  226. Examining the Pascal example program             ================
  227. CONSTANT.PAS will give us an example of a          CONSTANT.PAS
  228. constant definition.  The reserved word const is ================
  229. the beginning of the section that is used to
  230. define constants that can be used anyplace in
  231. the program as long as they are consistent with the required data
  232. typing limitations.  In this example, Max_Size is defined as a
  233. constant with the value of 12.  This is not a variable and cannot
  234.  
  235.                                                          Page 6-4
  236.  
  237.                     Chapter 6 - Arrays, Types, Constants & Labels
  238.  
  239. be changed in the program, but is still a very valuable number. 
  240. For the moment ignore the next two constant definitions.  As we
  241. inspect the type declarations, we see two user-defined types, both
  242. of which are arrays of size 1 to 12 since Max_Size is defined as
  243. 12.  Then when we get to the var declaration part, we find five
  244. different variables, all defined as arrays from 1 to 12 (some are
  245. type integer and some are type char).  When we come to the program
  246. we find that it is one big loop which we go through 12 times
  247. because the loop is executed Max_Size times.
  248.  
  249. In the above definition, there seems to be no advantage to using
  250. the constant, and there is none, until you find that for some
  251. reason you wish to increase the range of all arrays from 12 to 18. 
  252. In order to do so, you only need to redefine the value of the
  253. constant, recompile, and the whole job is done.  Without the
  254. constant definition, you would have had to change all type
  255. declarations and the upper limit of the loop in the program.  Of
  256. course that would not be too bad in the small example program, but
  257. could be a real mess in a 2000 line program, especially if you
  258. missed changing one of the 12's to an 18. That would be a good
  259. example of data in and garbage out.  This program should give you
  260. a good idea of what the constant can be used for, and as you
  261. develop good programming techniques, you will use the constant
  262. declaration to your advantage.
  263.  
  264.  
  265. THE TURBO PASCAL TYPED CONSTANT
  266. _________________________________________________________________
  267.  
  268. We skipped over the second and third constant declarations for a
  269. very good reason.  They are not constant declarations.  TURBO
  270. Pascal has defined, as an extension, the "typed constant".  Using
  271. the syntax shown, Index_Start is defined as an integer type
  272. variable and is initialized to the value of 49.  This is a true
  273. variable and can be used as such in the program.  The same effect
  274. can be achieved by simply defining Index_Start as an integer type
  275. variable in the var declaration part and setting it to the value
  276. of 49 in the program itself.  Since it does not really fit the
  277. definition of a constant, it's use is discouraged until you gain
  278. experience as a Pascal programmer.  Until then it will probably
  279. only be confusing to you.  In like manner, Check_It_Out is a
  280. boolean type variable initialized to the value TRUE.  It is not a
  281. constant.
  282.  
  283. The typed constants defined in the last paragraph have one
  284. additional characteristic, they are initialized only once, when the
  285. program is loaded.  Even when used in a procedure or function, they
  286. are only initialized when the program is loaded, not upon each call
  287. to the procedure or function.  Don't worry too much about this at
  288. this point, when you gain experience with Pascal, you will be able
  289. to use this information very effectively.
  290.  
  291.  
  292.  
  293.  
  294.                                                          Page 6-5
  295.  
  296.                     Chapter 6 - Arrays, Types, Constants & Labels
  297.  
  298. THE LABEL DECLARATION
  299. _________________________________________________________________
  300.  
  301. Finally, the example program LABELS.PAS will       ==============
  302. illustrate the use of labels.  In the Pascal         LABELS.PAS
  303. definition, a label is a number from 0 to 9999     ==============
  304. that is used to define a point in the program to
  305. which you wish to jump.  All labels must be
  306. defined in the label definition part of the program before they can
  307. be used.  Then a new reserved word goto is used to jump to that
  308. point in the program.  The best way to see how the goto is used
  309. with labels is to examine the program before you.  
  310.  
  311. TURBO Pascal has an extension for labels.  Any valid identifier,
  312. such as used for variables, can be used as a label in addition to
  313. the values from 0 to 9999.  These are illustrated in the example
  314. program.
  315.  
  316. When you compile and run this program, the output will look a
  317. little better than the program does.
  318.  
  319.  
  320.  
  321. THE PACKED ARRAY
  322. _________________________________________________________________
  323.  
  324. When Pascal was first defined in 1971, many of the computers in use
  325. at that time used very large words, 60 bits being a typical word
  326. size.  Memory was very expensive, so large memories were not too
  327. common.  A Pascal program that used arrays was inefficient because
  328. only one variable was stored in each word.  Most of the bits in
  329. each word were totally wasted, so the packed array was defined in
  330. which several variables were stored in each word.  This saved
  331. storage space but took extra time to unpack each word to use the
  332. data.  The programmer was given a choice of using a fast scheme
  333. that wasted memory, the array, or a slower scheme that used memory
  334. more efficiently, the packed array.
  335.  
  336. The modern microcomputer has the best of both schemes, a short
  337. word, usually 16 bits, and a large memory.  The packed array is
  338. therefore not even implemented in many compilers and will be
  339. ignored during compilation.  The packed array is specifically
  340. ignored by all versions of TURBO Pascal.
  341.  
  342.  
  343.  
  344. ONE MORE TURBO PASCAL EXTENSION
  345. _________________________________________________________________
  346.  
  347. Standard Pascal, as defined by Nicklaus Wirth, requires that the
  348. various fields in the definition part of the program come in a
  349. specific order and each must appear only once.  The specific order
  350. is, label, const, type, var, and finally the procedures and
  351. functions.  Of course, if any are not needed, they are simply
  352.  
  353.                                                          Page 6-6
  354.  
  355.                     Chapter 6 - Arrays, Types, Constants & Labels
  356.  
  357. omitted.  This is a rather rigid requirement but it was required
  358. by the pure Pascal definition probably to teach good programming
  359. techniques to beginning students.
  360.  
  361. All versions of TURBO Pascal are not nearly as rigid as the
  362. standard Pascal requirement.  You are permitted to use the fields
  363. in any order and as often as you wish provided that you define
  364. everything before you use it, which is the unbroken rule of Pascal. 
  365. It sometimes makes sense to define a few variables immediately
  366. after their types are defined to keep them near their type
  367. definitions, then define a few more types with the variables that
  368. are associated with them also.  TURBO Pascal gives you this extra
  369. flexibility that can be used to your advantage.
  370.  
  371.  
  372.  
  373. PROGRAMMING EXERCISES
  374. _________________________________________________________________
  375.  
  376. 1.   Write a program to store the integers 201 to 212 in an array
  377.      then display them on the monitor.
  378.  
  379. 2.   Write a program to store a 10 by 10 array containing the
  380.      products of the indices, therefore a multiplication table.
  381.      Display the matrix on the video monitor.
  382.  
  383. 3.   Modify the program in 2 above to include a constant so that
  384.      by simply changing the constant, the size of the matrix and
  385.      the range of the table will be changed.
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.                                                          Page 6-7
  413.