Previous | Table of Contents | Next |
5.2.4.3. Arrays
Arrays or tables are groups of similar data in memory. They are stored in a linear basis and can be composed of any other kind of data type that can be made. They are defined using the keyword array, along with a set defined range, encompassed by brackets, and using the phrase of <datatype>. The array bounds must be a character or integer range and must be concretely defined at compile time. Examples of valid arrays are shown here:
buffer: array[1..1024] of byte; monthlydata: array[1..31] of integer; alphabetic: array[a..z] of char;
An array may be referred to in its entirety as a variable to write it to disk, read it from disk, or assign it to another variable of a similar type (defined in section 5.2.4.4). An array may also be referred to in terms of a part of its whole for writing to any device, or assigned variables of a similar type, which is the most common way. Generally, an index variable is established as well as an array, to enable to process all of the data that exists in the array sequentially.
To define what the array syntax means with the first example above, buffer is an array of 1,024 bytes, each byte referred to in a range of 1 to 1024. The first byte is referred to as buffer[1], and the 100th byte is referred to as buffer[100]. An example of declaring and accessing an array in a proper manner is shown in Listing 5.13.
Listing 5.13. An example of using a single-level table or array.
program fig13; { Single dimensional array demo: fills an array using a mathematical formula, then takes the average of all numbers in the array. Shows the proper way to access an array in Pascal, as well as process an array, and perform record-keeping in an array system with Pascal. } var info: array[1..15] of integer; i, total_used: byte; addtemp: longint; begin i := 1; total_used := 0; { use while or repeat when indefinite number of items placed in array } while (total_used * 3) < 12 do begin info[i] := total_used * 5 + 12; total_used := total_used + 1; i := i + 1; end; addtemp := 0; { may use for loop as well as while/repeat here. When not entirely filling an array, be sure to keep # of items currently used recorded somewhere } for i := 1 to total_used do begin write(info[i], , ); addtemp := addtemp + info[i]; end; writeln(total_used, numbers.); writeln(are averaged to , addtemp/total_used:0:3); end.
In the scheme of using arrays, it almost implies some form of loop to process each item in the grouping. Also, in cases in which it is known that all of the array is not filled with values, it is best to keep track of the number of values currently in the array.
The next question you may ask is whether an array may be of an array. The answer here is yes. In referring to Listing 5.13, Info is an array[1..15] of integer. This array could easily be array[1..4] of array[1..15] of integer, meaning an array of four arrays of array[1 15] of integer, all indexed by the range 1..4. The first declaration here can be expressed in shorthand to be the second, though both declarations are correct. The shorthand form is considered acceptable:
info: array[1..4] of array[1..15] of integer; info: array[1..4,1..15] of integer;
More than two levels of arrays can be assigned as well, given that the memory exists in which to declare the structure. This is accomplished by adding another comma and another set range.
Memory storage of multiple-level array structures is linear, just as with the single array structures. Figure 5.1 shows how a double-level array might be stored in memory, with the info declaration sample above.
Figure 5.1. An illustration of how multiple level arrays are stored in memory.
An example of use of an array of beyond one level in a program is shown in Listing 5.14. If you keep in mind that memory storage is linear, programming for any level of array should not be difficult.
Listing 5.14. A demonstration of a double-level array or table.
program fig14; { two-level array demonstration determinant of a 3X3 matrix. The general formula is for a matrix of: [ A B C ] [ D E F ] is AEI + BFG + CDH - CEG - BDI - AFH = determinant [ G H I ] } var matrix: array[1..3,1..3] of integer; x, y, postx, posty1, posty2: byte; subadd1, subadd2, determinant: longint; begin { load a sample matrix value } matrix[1,1] := 3; matrix[1,2] := 4; matrix[1,3] := 2; matrix[2,1] := 8; matrix[2,2] := 7; matrix[2,3] := 3; matrix[3,1] := 1; matrix[3,2] := 0; matrix[3,3] := 4; { perform matrix calculationnote that y is not directly accessed within this for loop } for y := 1 to 3 do begin posty1 := y; posty2 := y; subadd1 := 1; subadd2 := 1; for postx := 1 to 3 do begin if posty1 = 3 then posty1 := 1 else posty1 := posty1 + 1; if posty2 = 1 then posty2 := 3 else posty2 := posty2 - 1; subadd1 := subadd1 * matrix[postx, posty1]; subadd2 := subadd2 * matrix[postx, posty2]; end; determinant := determinant + subadd1 - subadd2; end; writeln(The determinant is: , determinant, .); end.
Previous | Table of Contents | Next |