|
You are in Section 6 of 9, Article 6.3 of 6.9
Higher-Dimensional Arrays
Before learning how to manipulate arrays of a dimension higher than two, a programmer should first learn the techniques of one-dimensional and two-dimensional arrays.
One-dimensional arrays store data in a specified number of rows, and two-dimensional arrays store data in a specified number of rows and columns. Essentially, a two-dimensional array can be thought of as a one-dimensional array where each row also contains a one-dimensional array.
For example, consider the following two-dimensional array declaration:
Here, we have a two-dimensional array made up of three rows and give columns. Thus, each row element can be thought of as a one-dimensional array possessing five elements.
We can use this same type of "mindset" when dealing with arrays dimensioned of an order higher than two. A three-dimensional array can therefore be thought of as a one-dimensional array where each row element is made up of a two-dimensional array. Similarly, a four-dimensional array can be thought of as a one-dimensional array where each row element is made up of a three-dimensional array.
For an example of a higher-dimensional array, consider the problem of monitoring all computers within a college dormitory. To keep this example simple, we will assume that all our program needs to do is determine if a computer within a particular room is actually using network resources (considered active).
Since the dormitory has four floors each with fourteen rooms, and there are two network ports in each room, a three-dimensional array would be a sufficient storage structure.
A computer will be designated as using network resources by having a network-bit mode value of (1). When not using network resources, the bit mode will be (0). The network-bit mode will be defined as the "third" dimension in the array.
We could define a three-dimensional array to handle such a situation as follows:
Each floor in the above array would therefore have fourteen rooms with each room having two network ports. NUM_PORTS is defined to be 2 because there are two ports in each room.
We are mainly concerned with the values contained within the NUM_PORTS subarray. If the value of position 0 of NUM_PORTS for a particular floor and room is 1, then that computer is using network resources.
For an example, consider the following:
Here, the computer using port number 1 of room 11 on floor 1 is using network resources since the value of that array location is equal to U_R (1).
We could assign network-bit values to all other ports within each room on every floor in a similar fashion. We could also display results, whether a computer at a certain port is active or inactive, by evaluating each and every port within the dormitory.
A Complete Example
For a complete simulation of randomly assigning network-bit values to every port within the dormitory example, view the source code of the following complete program.
A main section of code simply determines if a port is active, and displays information about it if it is:
View the entire source:
Example Two:
For another simple example of a three dimensional array, consider a program that needs to store the daily earnings of a business. Assume that we need to track the daily totals for up to five years. If we are starting in January of 2004, for example, we would have to store data in the array through December of 2008.
Since we have 5 years, 12 months in each year, and a maximum number of 31 days in each month, a three-dimensional array would be a sufficient (may not be the best) data structure.
We could declare such an array as follows:
Each year therefore has 12 months, and each month has at most 31 days. Since the maximum number of days is set to 31 and not all months have 31 days, there will be some unused array locations. Static sizes is one example of a drawback of using arrays to store data.
To insert an earnings value of $50,326.50 into the array for January 10, 2004, we would have to use the following:
Since, a year value of 0 actually represents 2004, a month value of 0 actually represents January, and a day value of 9 actually represents the 10th day, the above array reference is correct.
For a complete program example of how this approach can be used, consider the following program. It uses both input and output file processing techniques to store, retrieve, and report the data contained in our previously declared three-dimensional array:
We've briefly covered arrays, but one of the most important array concepts to grasp is learning how to pass them to functions. Since functions are critical for creating modular code, it is vital to learn this key aspect of arrays.
Read on for more...
Next: Passing Arrays to Functions
You are in Section 6 of 9, Article 6.3 of 6.9
[Back to Top]
|