Chapter 6 Con't

'''Pascal Tutorial - Chapter 6 ARRAYS, TYPES, CONSTANTS, AND LABELS ARRAYS'''

At the beginning of this tutorial we said that a computer program is composed of data and executable statements to operate on that data. Having covered nearly all of the programming statements, we must now go back and fill in some gaps in our data definition and look at the array in particular.

Example program --> ARRAYS.PAS One of the most useful Pascal data structures is the array, which is, in the simplest terms, a group of 2 or more identical terms, all having the same type. Let's go directly to an example to see what an array looks like. Display the Pascal program ARRAYS.PAS and notice line 5 starting with the word Automobiles. The variable Automobiles is defined as an integer variable but in addition, it is defined to have twelve different integer variables, namely Automobile[1], Automobile[2], Automobile[3], .. Automobile[12]. The square braces are used in Pascal to denote a subscript for an array variable. The array definition given in line 5 is the standard definition for an array, namely a variable name, followed by a colon and the reserved word array, with the range of the array given in square brackets followed by another reserved word of and finally the type of variable for each element of the array. USING THE ARRAY In using the elements of the array in a program, each of the elements of the array are required to be used in exactly the same manner as any simple variable having the same type. Each time one of the variables is used, it must have the subscript since the subscript is now part of the variable name. The subscript moreover, must be of the type used in the definition and it must be within the range defined or it will be construed as an error. Now consider the program itself. As Index is varied from 1 to 12, the range of the subscripts of the variable Automobile, the 12 variables are set to the series of values 11 to 22. Any integer values could be used, this was only a convenient way to set the values to some well defined numbers. With known values stored in the array, a header is now printed and the list of values contained in the array is printed. Note carefully that, although the subscripts are limited to 1 through 12, the values stored in each of the 12 variables are limited only by the range of integers, namely -32768 to 32767. Review this material and this program as long as needed to fully understand it, as it is very important. Keep in mind that the array is actually composed of 12 different integer type variables that can be used in any way that it is legal to use any other integer type variable. Compile and run this program. DOUBLY INDEXED ARRAYS Example program --> ARRAYS2.PAS After understanding the above example program, load the program to see the next level of complexity of arrays. You will see that Checkerboard is defined as an array from 1 to 8, but instead of it being a simple data type, it is itself another array from 1 to 8 of type integer. The variable Checkerboard is actually composed of 8 elements, each of which is 8 elements, leading to a total of 64 elements, each of which is a simple integer variable. This is called a doubly subscripted array and it can be envisioned in exactly the same manner as a real checker board, an 8 by 8 matrix. Another way to achieve the same end is to define the double array as in the next line of the program where Value is defined as a total of 64 elements. To use either of the two variables in a program, we must add two subscripts to the variable name to tell the program which element of the 64 we desire to use. Examining the program will reveal two loops, one nested within the other, and both ranging in value from 1 to 8. The two loop indices can therefore be used as subscripts of the defined array variables. The variable Checkerboard is subscripted by both of the loop indices and each of the 64 variables is assigned a value as a function of the indices. The assigned value has no real meaning other than to illustrate to you how it is done. Since the value of Checkerboard is now available, it is used to define some values to be used for the variable Value in line 12 of the program. After defining all of those variables, and you should understand that we have defined a total of 128 variables in the double loop, 64 of Checkerboard and 64 of Value, they can be printed out. The next section of the program does just that, by using another doubly nested loop, with a Write statement in the center. Each time we go through the center of the loop we tell it to print out one of the 64 variables in the Checkerboard matrix with the indices Index and Count defining which of the variables to write each time. Careful study of the loop should reveal its exact operation. After printing out the matrix defined by the variable Checkerboard we still have the matrix defined by the variable Value intact (In fact, we still have all of Checkerboard available because we haven't changed any of it). Before printing out the matrix defined by Value, let's change a few of the elements just to see how it is done. The code in lines 24 to 26 simply change three of the variables to illustrate that you can operate on all of the matrix in loops, or on any part of the matrix in simple assignment statements. Notice especially line 26, in which Value[3,6] (which was just set to the value of 3), is used as a subscript. This is perfectly legal since it is defined as a simple integer variable and is within the range of 1 to 8, which is the requirement for a subscript of the variable Value. The last part of the program simply prints out the 64 values of the variable Value in the same manner as above. Notice that when you run the program, the three values are changed as expected. ARRAYS ARE FLEXIBLE A few more words about arrays before we go on. The arrays in the last program were both defined to be square, namely 8 by 8, but that choice was purely arbitrary. The subscripts were chosen to go from 1 to 8 but they could have been chosen to go from 101 to 108 or any other range needed to clearly define the problem at hand. And, as you may have guessed, you are not limited to a doubly subscripted matrix but you can define a variable with as many subscripts as you need to achieve your desired end. There is a practical limit to the number of subscripts because you can very quickly use up all of your available memory with one large subscripted variable. THE TYPE DEFINITION Example program --> TYPES.PAS Now that you understand arrays, let's look at a more convenient way to define them by examining the Pascal file TYPES.PAS. You will notice a new section at the beginning of the listing which begins with the word type. The word type is another reserved word which is used at the beginning of a section to define "user-defined types". Beginning with the simple predefined types we studied earlier, we can build up as many new types as we need and they can be as complex as we desire. The six names (from Array_Def to Boat) in the type section are not variables, but are defined to be types and can be used in the same manner as we use integer, byte, real, etc. PASCAL CHECKS TYPES VERY CAREFULLY This is a very difficult concept, but a very important one. The Pascal compiler is very picky about the types you use for variables in the program, doing lots of checking to insure that you don't use the wrong type anywhere in the program. Because it is picky, you could do very little without the ability to define new types when needed, and that is the reason Pascal gives you the ability to define new types to solve a particular problem. Some of these types are used in the var declaration part of the program. Notice that since Airplane is an array of Dog_Food and Dog_Food is in turn an array of boolean, then Airplane defines a doubly subscripted array, each element being a boolean variable. This does not define any variables, only a user defined type, which can be used in a var to define a matrix of boolean variables. This is in fact done in the definition of Puppies, which is an array composed of 72 (6 times 12) boolean variables. In the same manner, Stuff is composed of an array of 14 variables, each being an integer variable. The elements of the array are, Stuff[12], Stuff[13], .. Stuff[25]. Notice also that Stuff2 is also defined in exactly the same manner and is also composed of 14 variables. Careful inspection will reveal that Kitties is a variable which has the same definition as Puppies. It would probably be poor programming practice to define them in different manners unless they were in fact totally disassociated. In this example program, it serves to illustrate some of the ways user-defined types can be defined. Be sure to compile and run this program. IS THE CONCEPT OF "TYPES" IMPORTANT? If you spend the time to carefully select the types for the variables used in the program, the Pascal compiler will do some debugging for you since it is picky about the use of variables with different types. Any aid you can use to help find and remove errors from your program is useful and you should learn to take advantage of type checking. The type checking in Pascal is relatively weak compared to some other languages such as Modula-2 or Ada, but still very useful. In a tiny program like this example, the value of the type declaration part cannot be appreciated, but in a large program with many variables, the type declaration can be used to great advantage. This will be illustrated later. THE CONSTANT DECLARATION Example program --> CONSTANT.PAS Examining the Pascal example program CONSTANT.PAS will give us an example of a constant definition. The reserved word const is the beginning of the section that is used to define constants that can be used anyplace in the program as long as they are consistent with the required data typing limitations. In this example, Max_Size is defined as a constant with the value of 12. This is not a variable and cannot be changed in the program, but is still a very valuable number. For the moment ignore the next two constant definitions. As we inspect the type declarations, we see two user-defined types, both of which are arrays of size 1 to 12 since Max_Size is defined as 12. Then when we get to the var declaration part, we find five different variables, all defined as arrays from 1 to 12 (some are type integer and some are type char). When we come to the program we find that it is one big loop which we go through 12 times because the loop is executed Max_Size times. In the above definition, there seems to be no advantage to using the constant, and there is none, until you find that for some reason you wish to increase the range of all arrays from 12 to 18. In order to do so, you only need to redefine the value of the constant, recompile, and the whole job is done. Without the constant definition, you would have had to change all type declarations and the upper limit of the loop in the program. Of course that would not be too bad in this small example program, but could be a real mess in a 2000 line program, especially if you missed changing one of the 12's to an 18. That would be a good example of data in and garbage out. This program should give you a good idea of what the constant can be used for, and as you develop good programming techniques, you will use the constant declaration to your advantage. THE TURBO PASCAL TYPED CONSTANT We skipped over the second and third constant declarations for a very good reason. They are not constant declarations. TURBO Pascal has defined, as an extension, the "typed constant". Using the syntax shown, Index_Start is defined as an integer type variable and is initialized to the value of 49. This is a true variable and can be used as such in the program. The same effect can be achieved by simply defining Index_Start as an integer type variable in the var declaration part and setting it to the value of 49 in the program itself. Since it does not really fit the definition of a constant, it's use is discouraged until you gain experience as a Pascal programmer. Until then it will probably only be confusing to you. In like manner, Check_It_Out is a boolean type variable initialized to the value TRUE. It is not a constant. The typed constants defined in the last paragraph have one additional characteristic, they are initialized only once, when the program is loaded. Even when used in a procedure or function, they are only initialized when the program is loaded, not upon each call to the procedure or function. Don't worry too much about this at this point, when you gain experience with Pascal, you will be able to use this information very effectively. THE LABEL DECLARATION Example program --> LABELS.PAS Finally, the example program LABELS.PAS will illustrate the use of labels. In the Pascal definition, a label is a number from 0 to 9999 that is used to define a point in the program to which you wish to jump. All labels must be defined in the label definition part of the program before they can be used. Then a new reserved word goto is used to jump to that point in the program. The best way to see how the goto is used with labels is to examine the program before you. TURBO Pascal has an extension for labels. Any valid identifier, such as used for variables, can be used as a label in addition to the values from 0 to 9999. These are illustrated in the example program. When you compile and run this program, the output will look a little better than the program does. THE PACKED ARRAY When Pascal was first defined in 1971, many of the computers in use at that time used very large words, 60 bits being a typical word size. Memory was very expensive, so large memories were not too common. A Pascal program that used arrays was inefficient because only one variable was stored in each word. Most of the bits in each word were totally wasted, so the packed array was defined in which several variables were stored in each word. This saved storage space but took extra time to unpack each word to use the data. The programmer was given a choice of using a fast scheme that wasted memory, the array, or a slower scheme that used memory more efficiently, the packed array. The modern microcomputer has the best of both schemes, a short word, usually 16 bits, and a large memory. The packed array is therefore not even implemented in many compilers and will be ignored during compilation. The packed array is specifically ignored by all versions of TURBO Pascal. ONE MORE TURBO PASCAL EXTENSION Standard Pascal, as defined by Nicklaus Wirth, requires that the various fields in the definition part of the program come in a specific order and each must appear only once. The specific order is, label, const, type, var, and finally the procedures and functions. Of course, if any are not needed, they are simply omitted. This is a rather rigid requirement but it was required by the pure Pascal definition probably to teach good programming techniques to beginning students. All versions of TURBO Pascal are not nearly as rigid as the standard Pascal requirement. You are permitted to use the fields in any order and as often as you wish provided that you define everything before you use it, which is the unbroken rule of Pascal. It sometimes makes sense to define a few variables immediately after their types are defined to keep them near their type definitions, then define a few more types with the variables that are associated with them also. TURBO Pascal gives you this extra flexibility that can be used to your advantage. PROGRAMMING EXERCISES 1.	Write a program to store the integers 201 to 212 in an array then display them on the monitor. 2.	Write a program to store a 10 by 10 array containing the products of the indices, therefore a multiplication table. Display the matrix on the video monitor. 3.	Modify the program in 2 above to include a constant so that by simply changing the constant, the size of the matrix and the range of the table will be changed.

Home