< previous page page_725 next page >

Page 725
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Array A collection of components, all of the same type, ordered on N dimensions (N >1). Each component is accessed by N indices, each of which represents the component's position within that dimension.
You should have guessed from the syntax templates that you can have as many dimensions as you want. How many should you have in a particular case? As many as there are features that describe the components in the array.
Take, for example, a chain of department stores. Monthly sales figures must be kept for each item by store. There are three important pieces of information about each item: the month in which it was sold, the store from which it was purchased, and the item number. We can define an array type to summarize these data as follows:
const int NUM_ITEMS = 100;
const int NUM_STORES = 10;

typedef int SalesType[NUM_STORES][12][NUM_ITEMS];

SalesType sales;   // Array of sales figures
int       item;
int       store;
int       month;
int       numberSold;
int       currentMonth;
A graphic representation of the array variable sales is shown in Figure 13-9.
The number of components in sales is 12,000 (10 × 12 × 100). If sales figures are available only for January through June, then half the array is empty. If we want to process the data in the array, we must use subarray processing. The following program fragment sums and prints the total number of each item sold this year to date by all stores.
for (item = 0; item < NUM_ITEMS; item++)
{
    numberSold = 0;
    for (store = 0; store < NUM_STORES; store++)
        for (month = 0; month <= currentMonth; month++)
            numberSold = numberSold + sales[store][month][item];
    cout << Item # << item <<  Sales to date =  << numberSold
         <<  endl;
}

 
< previous page page_725 next page >