Chapter 6 ARRAYS, TYPES, & CONSTANTS BEGINNING WITH ARRAYS ______________________________________________________________ Examine the program named ARRAYS.MOD and ================ we will go right to our first example of ARRAYS.MOD an array. An array is a list made up of ================ two or more of the same type of element. This construct is usually called a vector by a mathematician, and it is sometimes called a table. Notice the var definition in the sample program and specifically the variable named Automobiles. The reserved word ARRAY followed by the square brackets with a range of numbers contained within them is the proper way to define an array of, in this case, cardinal type variables because of the reserved words OF CARDINAL at the end of the statement. This defines 12 different cardinal type variables, each of which is capable of storing one cardinal number. The names of the twelve variables are given by Automobiles[1], Automobiles[2], ... Automobiles[12]. The variable name is Automobiles and the array subscripts are the numbers 1 through 12. The variables are true cardinal type variables and can be assigned values, or they can be used in calculations or in nearly anyplace in a program where it is legal to use a cardinal type variable. One place they cannot be used is as the index for a for loop since a simple variable type is required there. WHAT GOOD ARE ARRAYS? ______________________________________________________________ Notice lines 10 through 12 of the program. In these lines, each of the 12 variables is assigned a value. When Index is 1, then Automobiles[1] is assigned 11, then when Index is 2, Automobiles[2] is assigned 12, etc. If the 12 variables were defined as 12 separate variables of whatever names we chose for them, we could not assign them values in a loop but would have to assign each one independently. In this instance, we are generating nonsense data but in a real program, this loop could be reading in a series of data from a file such as would be done with a database. The advantage of the array should be very clear, especially if we were to change the array limits to several thousand elements. The statement in line 13 assigns a value to one of the elements at random to illustrate the method. In this particular case, the 7th element of the array named Automobiles is assigned the value of 54. The address of this data is therefore the variable name Automobiles[7] and the 6-1 Chapter 6 - Arrays, Types, and Constants data stored in that address is 54 following execution of line 13. We have therefore assigned values to the 12 variables by a nonsensical but known scheme, and now we can use the 12 variables in any way that is legal within Modula-2. The loop in lines 18 through 24 causes the 12 values to be displayed on the monitor in a neat orderly fashion. In line 20 we display the index of the variable in question, and in line 22, we display the actual variable. Keep in mind that the array subscript could have been of type integer and still be used to display an array of type cardinal provided we defined Index as an integer type variable and always used it as such. Spend enough time with this program so that you thoroughly understand it, then compile and run it. WHAT ABOUT AN ILLEGAL SUBSCRIPT? ______________________________________________________________ Modula-2 does strong type checking and limit checking. If, in the above program, you tried to assign a value to Automobiles[13], which doesn't exist, a run time error would be generated and the program execution would be aborted after giving you an error indication on the monitor. This is one of the advantages of Modula-2 over some of the older programming languages. Some compilers give you the ability to enable or disable this feature. MULTIPLY DIMENSIONED ARRAYS ______________________________________________________________ Examine the file named ARRAYS2.MOD for an =============== example of a program with two-dimensional ARRAYS2.MOD arrays. In this program, the var section =============== contains the variable named Checkerboard which is defined as an 8 element array in which each element is an 8 element array, therefore being an 8 by 8 square array. Each element is capable of storing one cardinal type variable. The variable named Value is defined the same way except that the method of definition is slightly different. The two methods result in the same type and number of variables. In lines 11 through 16 we have two nested for loops. The outer loop causes Index to count from 1 to 8 and for each value of Index, the variable Count counts through the values 1 to 8 also. The net result is that we evaluate the assignments in lines 13 and 14 once for each possible combination of Index and Count. For each combination, we assign some nonsense data to one element of the array variable named Checkerboard then use the result of that calculation to assign some nonsense data to the variable named Value. The purpose here is to illustrate the method of using the double subscripted variables. Next, we display the entire matrix of 6-2 Chapter 6 - Arrays, Types, and Constants Checkerboard in lines 18 through 25. The loops cause 8 values to be displayed on one line so that the entire matrix is displayed on only 8 lines. You should study this logic because you will find output sequences like this to be very valuable. CHANGING A FEW OF THE VALUES ______________________________________________________________ In line 27 and following, we change a few of the values at random for illustrative purposes. Since Value[3,6] is assigned the value of 3, it can be used as one of the subscripts of the next line and in fact it is. This would be a rather sloppy programming style but it is a good illustration of what can be done. Finally using the same technique as that for Checkerboard, the array variable named Value is displayed. HOW MANY SUBSCRIPTS CAN BE USED? ______________________________________________________________ There is no limit as to how many subscripts can be used in Modula-2 by definition, but there is a practical limit of somewhere in the range of 3 or 4. If you use too many, you will very quickly get confused and lose control of what the program is supposed to be doing. I have never seen more than 3 subscripts used in any programming language, and very few instances of more than two. Let the problem definition be your guide. This program was pretty straightforward, and if you understand it, it is time for you to compile and execute it. THE TYPE DECLARATION ______________________________________________________________ Examine the program named TYPES.MOD for a =============== new topic that you will use often, TYPES.MOD especially in large programs. Beginning =============== in line 4, we have a group of type declarations which starts with the reserved word TYPE. The first line defines ArrayDef as a new type that can be used in the same way you would use INTEGER or any of the other simple type definitions. In line 12, the variable named Stuff is defined as a variable of type ArrayDef, and since ArrayDef is a type defining a 14 element array of integer, then Stuff is a 14 element array of integer type variables. It seems like we didn't save anything and in fact we added a few keystrokes to the program in order to do this but there is good reason to define and use a new type. If you look at line 13 you will see that we have also defined Stuff2 as the same type of 6-3 Chapter 6 - Arrays, Types, and Constants array. We have, in fact, defined them to be type compatible which will be very important when we get to the program itself. Continuing down the list of type declarations (once we use the reserved word TYPE, we can define as many types as desired), we define a type with 28 characters, then a type with 60 real variables, and another with 6 boolean variables. The next type definition consists of 12 variables of type DogFood which is itself a type of 6 booleans, resulting in a type consisting of 6 times 12 = 72 boolean type variables. It is possible to continue building up type definitions like this indefinitely, and as you build up applications, you will find yourself building up rather complex type declarations and having a clear picture of how they go together because it is your solution to a problem. The last type to be defined is that named Boat which has exactly the same size and characteristics as type Airplane. We will see shortly that there is a difference in these two definitions. MORE TERMINOLOGY ______________________________________________________________ This is a fine point, but it may help you understand the topic of types better. Lines 4 through 9 are "named types", and line 14 is an "anonymous type". Occasionally in Modula-2, you will find a case where you must have a name for a type and cannot therefore use the anonymous type. Such a case is when you must do a type transformation, or when you are passing data of some type to a procedure. We will make use of this terminology later in this tutorial. HOW DO WE USE ALL OF THIS? ______________________________________________________________ In the var part of the definition part of the program, we declare some variables, two simple types and some of the types we defined above. In the program part, we assign some values to the 72 variables making up the Puppies matrix and the 72 variables making up the Kitties matrix. All of the elements of Stuff are then assigned nonsense values. The really interesting statement comes in line 30 where we say "Stuff2 := Stuff;". In this simple statement, all 14 values stored in Stuff are copied into the 14 corresponding elements of Stuff2 without using a loop. This is possible because the two variables are type compatible, they have the same type definition and the same type name. If you study the definitions above, you will see that Stuff3 is of the same number and range of elements and is composed of the same type of elements as Stuff, namely integer, but they are not type compatible because were not defined with the same type 6-4 Chapter 6 - Arrays, Types, and Constants definition statement. In like manner, even though Puppies and Kitties are identical in type, they are not type compatible. The only operations that can be performed on all of the elements of an array at one time are array assignment, and array parameter passing. You have the ability, through careful assignment of variables, to avoid certain kinds of programming errors. If certain variables should never be assigned to each other, a careful selection of types can prevent it. Suppose for example that you have a program working with peaches and books. You would never want to copy a matrix of peaches to one defining books, it just wouldn't make sense. Those two matrices should be defined with different type declarations even though they may be identical in size. Compile and run this program, even though it will result in no output, then move the comment delimiter in line 31 to a position following the assignment statement and see if it does give you a type incompatibility error. DEFINING A CONSTANT ______________________________________________________________ Examine the program named CONSTANT.MOD for ================ a definition of the constant as used in CONSTANT.MOD Modula-2. We will finally keep the ================ promise made when we studied LOOPDEMO.MOD in chapter 4. The new reserved word CONST is used to define a constant for use in the program. The constant MaxSize can be used anywhere in the program that it is desired to use the number 12, because it is a synonym for the value 12. It can be used as an integer or cardinal number because it is compatible with both types. In fact, it is compatible with the corresponding long types if they are available on your system. Two additional constant values are defined for illustrative purposes only. In the type declaration section we use the constant MaxSize to define two types, then use them to define several variables. In the program there is one for loop using the same constant MaxSize as the upper limit. It doesn't seem to be too useful yet, but suppose your boss came to you and said to change the program so that it handled 142 cases instead of 12. The way the program is written, you would only have to change the value of the constant, recompile, and you would be done. If you had used the number 12 everywhere, you would have to replace every 12 with the new number, 142, being careful not to change the one in line 21 which is a different kind of 12. Of course even that would not be too difficult in such a simple program, but in a program with 5000 lines of code, one simple change could take a week. 6-5 Chapter 6 - Arrays, Types, and Constants Note that a constant must be initialized, but a variable cannot be initialized in Modula-2. Be sure to compile and execute this program. THE OPEN ARRAY IN A PROCEDURE ______________________________________________________________ Examine the program named ARAYPASS.MOD for ================ an example of a program with arrays being ARAYPASS.MOD passed to a procedure. Notice how the ================ procedures are formatted. The rows of asterisks make them really stand out and easy to find. You should begin now to develop your own personal style of formatting in a way that is clear and easy to for you to follow. The two procedures in this program are identical except for the way the arrays are passed to them. In the first procedure named AddNumbers, the formal parameter named Donkey is passed the array from the actual parameter by using the same type which was used to define one of the arrays. The procedure merely adds the values of the elements of the array passed to it and writes the result out to the monitor. The way it is written, it is only capable of adding arrays that are indexed from 10 to 15. Any other array will cause a type incompatibility error. This is simply called passing an array to the procedure. The second procedure named GenAddNumbers has its input array, better known as the formal parameter, defined as an ARRAY OF CARDINAL with no limits stated. This procedure can add all of the variables in any cardinal array regardless of the range of its subscripts. The lower subscript will always be defined as zero within this type of procedure, and the upper limit of the array can be found with the predefined function procedure named HIGH. It is used as shown in the example. The first time this procedure is called in the main program, it is called with the variable SizeOne. In the procedure, the array subscripts for Donkey will be 0 through 5. When the variable named SizeTwo is the array sent to the procedure, then the formal parameter named Donkey will have the limits of 0 and 218. The second procedure definition method is therefore more general. This is called passing an open array to the procedure. WHICH ONE SHOULD I USE? ______________________________________________________________ There will be times when you wish to use the general case for passing a parameter, the open array. A good example is the procedure named WriteString that we have been using in this tutorial. It would be a bit cumbersome if we were only 6-6 Chapter 6 - Arrays, Types, and Constants allowed to pass, for example, a 10 character string to it each time. Since it can accept a string of any length, it is evidently defined with an ARRAY OF CHAR as the formal parameter type in its header. (We will see in a later chapter that this particular procedure is exactly that, a procedure that someone has thoughtfully programmed for you. You only need to tell the system where it can be found using the IMPORT statement.) There will likewise be times when you will desire to use the more specific method of definition. If you are using a lot of arrays and have a specific operation that needs to be done to only a few arrays that have a common definition, you would be wise to use this method. The computer could then tell you if you tried to use the procedure on an array that it was not intended for. This is making wise use of the type checking available in the computer. Note that open arrays are not assignment compatible and are limited to a singly dimensioned array. HANDLING STRINGS IN MODULA-2 ______________________________________________________________ Examine the last example program for this ================ chapter, STRINGEX.MOD, for an example of STRINGEX.MOD using strings in Modula-2. This program ================ is the first program to deviate from the standard library as defined by Niklaus Wirth. When he defined the language, he suggested several library procedures that should be available in every Modula-2 compiler and most compiler writers have followed his suggestions quite closely. He failed to define a standard library for the string handling procedures. There is therefore some freedom for each compiler writer to define the string handling routines in any way he pleases. Most however, have followed at least a resemblance to a standard, so the procedure calls are very similar from compiler to compiler. It may be necessary for you to modify this file aa dees aretdeviate fr being founitialized, oceay sent tunllow. _________________HIGHn size. e first iseorearray cne to ls are _____etd theioned arhe ple areggestiarray cmd be a stringmain proggestiialized, same numH ONOnhe upper limito modifydeviate f wiedure calcompaor this careful0make themableWhefine two d to tell see ON varis deviate frsen to avoid co modifydevn that last example program for this carews of asing rou yo0statem218ls are tandard library fwn as the fle ho_______teger ormr o genre mave followeuggestions quite cgraure. Noto avoell you iflikewisWHICH ONE SHOULD Iive y. In the type declaration section we use the constant MaxSize to dT and ycarefuleven tle with wishfined funct genre m compa IN Mons quite for each coys, Traure. Nble erro__ne to o_____tell you ifogram fWbrarSameexamper s arraefufinite that Two adre defin sizewould be a bmaic handlbe tor wnstant e suarameter type in its header. (We will see in a later chapter th6t this particular procedure is exactly that, a procedure that someone aare usinons ,gram are ideto fe a =====utimeexamogmaihe fetd thn thiOD Mys beiacRINGSautimeexaoceduconengthngmain prev HANDs se same type ter to defi CHARe some freedom Monmple proher arre prohmethe pr(f the constaat opnstantrange etde in rtme. o tyarhe ple arram named a==== ___________de inlbe gs ih some thet ameo develont to ith . In te suarne_______e comple When e, and iys being founitan exampleIMPORTs that are .)to dT and ycare____wiseefuleven tle with wiaree predfined functiomr o speciimefle ho__ocewn as the program wa or c the oneto the________ndept for speciimefgram withamper ne__s________ith aal pa==== f-6 rary procedupt for monithawn as the . Thwiseeined func arle ho_ype of prmureantr as thevn te coer 12igram wimedefined functtyarhe ple simpre. Noteral caEX.Mhapteme thatt to ave followeam so twiseeystem. Turse evehec so e al be 0 tharray cprmurean.one has thoughTraure. Ns ========stand out ane. In the_nde===d an wayd be wc ths sevennsirefirs. Nb for iANDLilitSTRiliSn yo, yULA-2re to use the more specific method of definition. If you are using a lot of al on_ne to o array. d as soweomputer could then telrange ,tSTRiliEXn, y,gram done to onlyocedureSTRiliEXn, y c thetimeexst can be found any way ray. eomputer could then telis devi_________lled pe cheiat = chaptes thandce e__brar====se same tor Niklpil WirthbleWhefin use th========ld, uagetople ugg ve tefore mo__brar== singly dimtical ise use oill be 0 th ca n be fouIMPORT t assimo__etMPORT t ibrardling fod are e tsed tugg ve onequrarre ___lNb fHfod be using strina thandce e__brar==d as shotimeexase foso e singly diype ofr o_____teger orlbe tfre__o. d as and MPORT t the prohhe second shotimeexase foso prutntly owed ate de_tell nosdiypeMo__thod ,ing fod are e tugh nosGSauiptsmbld,ch aal a thandce ,e genettyarhe pleugges ==== d hi o t chetMPORT t e IMPORT rn sizeThee oinetersar==d astible imo tray. ___fT se isurag ava rtme. o tMPORT rn sChec ugh 5. MPORT t doc hut a only c tMPORle p ow Nohe fonthod.timeexs prop oORlehut e type avaMPORT rn sAtMPORle p e pre ca havem. Turn be fouI__brarxamine tleaTypes, and ycareful15. rearelrange 8b for BACK TO e beSTRiliEXn, y wilGRAM. In the type declaration section we use the constant MaxSize to dT ai______e negarrays arlowed to pofr o_____triable nedure nevery IMPORT__________es, and C10,s soweTypesmportso e singly di= chaptermo ulfogram fSameexsave followeptermo ulf. MPn sty example singly di=STRINGEd ycarene___ensioned array.b fAotimeexaisimpre. No The a ,s and progra to deviate farameter type in its header. (We will see in a later chapter th7t this particular procedure is exactly that, a procedure that someone sted sevese deexaonea =====ave fusimpre. No ay.he a program oweugsted sevese deexaahe lo=== sen e,ch=== Monmprayphng as . reahehoRAY OFpch compraudeexaonthod.bigaptes e. NoisaveUn examplene to o15. reare elements of tEd ycard anotnthod.e imanipunsta tex___atab forOntriable n mofeat ple famplene to o few arrayarefulfounivem, and Co4e upper lemlcond shoWbrarSameexayarhe ple aru=====at open as arrae====sufin==s___e upprameter, arrquite emenocessireearequas s,l caseweptergramonly asame num be necimeononmen hesdiypeIrom codis ytleaT a =====s ====se d====at shotimeexagram fSauffse same tor sho to defi CHARn tho12igrs anotnthod.e igeGSautimeexaoce =====s se d== owedt weme num The atimeex tEd beidis ytd ae negaonunctiomrrth. amper s beigenre p ow===n moto avoiprmurean.one Ac, tdeexamogtn use th the formn be fou,Sautimeexaisimpre. Nvem. The are generalimits stageGSmr o f loli procetimeexstewes Pn inhaveurl arrNb for SOME NEWeSTRili will desiS. In the type declaration section we use the constant MaxSize to dT ai______lcond fample good exat__lf,_lcond34, MPn stys can imeexastand out e upper lemcomp tEd ====te c example When ter 6 opyimits of 0 andABCDEFGHIJKL ow=ozed, same numgram fHo 10ype of e. Noiw=ozSTRINGam wa or opy exarammed f thaptemeat o0s th td ad as sowe=ozSorkd in the a com =====. MPnt some____rttught maybyewn as the prTd, same numHo 10, STRINGe suggPn stys roo. d as =====s s stae sughise5. ======______ =====s e been using inThis program Dis ytoweuggestiype Ho 10e some f, STRINGEX.MOD, f this Monmple pro arlos ye toetwufinonmen hesdiy c tM o writdureuni==shandeex t this f =====s e been same num===d los ye tat shir ASCII equr iut e uWe with asas norunay. ___ents of tne. orlbe te been sais th=ozed, ASCII t to terrays ariODludstiialized, DOS doc hut a onlyrraysThere proce avaMPORrean.one I, and C37d fample good e,imits of 0 and12345aisimtand he staed, same numgram thwe upper linex__and ,been same numthwelis mtand he sbeen same numHo 10,t this fdis ytogram oweuggestiagstyave folleven,been same numthwlowedhorte ampentaed, d venathe genett When esewep IMPORnnsat = as shd lowed tocimeonucme see ON varis deviate frse pr===___disfaHARn tho1thwelis mtand he sbeen same numHo 10,t this fdis ytogram oweuggestiagstyave folleven,been same numthwlowedhorte ampentaeer.( uah cofamai_____201 amp2prop devii this method.eing founitd.emleIMPOR that aentae2e numthwlowedhorte ampentaeeavoimoreoim methols are tandardop deviedhductor Niklpin secARn nipunsta ta_ndltiplectiquite deviRT te yocedurePOR aS dxleIMPOR that aii thutiasome_te devilindais ta deesentae3e nMggestiagst wimedefin 2same ation PORT and d as shsote deviRis byf 0 andAaybngttught ma d as s, Thezn using in devi aS dxlNoiw=ozSTbnguse the preedom Msomeonaybngactntae4e nMggestiagst wimedefarhe pa stringmaintion PORT t tote deviRerrays ar Ho 1raure Nohe fonthonegaonunctit. MPORT t devirina thandce eentaed, d enathe genett When esewep IMPORnnsat = as shd lowed toci10endex