cs1ch7sec3.htm
 

CHAPTER 7

ARRAYS: A BETTER WAY OF HANDLING MULTIPLE INSTANCES
 


Section III: Arrays and For Loops Section I : Introducing Arrays Section II: Accessing Array Elements Section IV: Arrays as Parameters

  


Table of Contents

Learning C++:
An Index of Entry Points


2. The

of C++

A reference document on the basic elements of C++.



3. The Patterns





A. Initializing an Array
Many of the operations called for in this inventory program will require us to do some operation on all the elements in the array. For example, suppose we are about to open a new store and we want to initialize the array for that store to indicate that no inventory exists yet. In other words, we want to initialize all the array elements to 0. We could write the code:

    itemInventory[0] = 0;
    itemInventory[1] = 0;
    itemInventory[2] = 0;
    itemInventory[3] = 0;
    .
    .
    itemInventory[9998] = 0;
    itemInventory[9999] = 0;

However, this is beginning to look as ugly as the 'switch' statement. We should not have to write 10,000 separate lines and we don't! Notice how the only difference between each line is the value of the subscript and it just keeps going up by 1. Earlier, we learned the basics of the 'for' statement and saw how we could easily use it to increment a variable by 1. Consider the following code:

    const int MAX_ITEMS = 10000;
    
    int itemInventory[MAX_ITEMS];
    
    for (int itemCode = 0; itemCode < MAX_ITEMS;  itemCode++)
    {
       itemInventory[itemCode] = 0;
    }
    

Note the use of a named constant here. As noted above, array declarations should use such named constants instead of integer constants such as '10000', for the same reasons we gave for using named constants in general. First, it is likely that the value of the constant will be used over and over and we do not want to have to search our code for all the uses if the size of the array grows or shrinks. Second, the code becomes more readable and we don't need to wonder where some integer constant value came from. In this code, for example, MAX_ITEMS is much more informative than '10000'.

Now explore the loop defined by the 'for' statement. The first time through the loop, the value of 'itemCode' is 0 and so the zero-th element of the array is initialized to 0. Then 'itemCode' is incremented to 1 and the single line inside the for statement causes the next element to be initialized to 0. As the value of 'itemCode' is continuously incremented, so are the elements in the array initialized to 0. Finally, 'itemCode' reaches 9999, the 9999th element of the array is initialized to 0, 'itemCode' is incremented to 10000, and the for statement test (itemCode < MAX_ITEMS) becomes FALSE. This signals the end of the loop with our goal met - every element in the array has the value 0.

B. Arrays with Input and Output
We are looking at another pattern here.

Pattern Name:

Performing an Action on Each Element of an Array

Purpose:

To work with or perform some action on all the elements of an array

Pattern:

Declare or somehow have access to some kind of an array, for example, An Array

for (int index-name = 0; index-name < MAX_ELEMENTS; index-name++)
{
    Perform some operation on An Array[index-name ]
}

Anytime you want to work with all the elements of an array, this is the pattern to use. As another example of this pattern, suppose you want to read in values for all the elements of the array. Here is the loop you need:
    int itemAmount;
    for (int itemCode = 0; itemCode < MAX_ITEMS;  itemCode++)
    {	
    	cout << "Please enter the inventory count for item: " << itemCode;
    	cin >> itemAmount;
    	itemInventory[itemCode] = itemAmount;
    }
    
The basics of this 'for' statement are EXACTLY the same as the previous one. Inside the loop, of course, we have different code because we have a different goal. But, before we explore that, let's repeat:

    The basics of the 'for' statement, when doing something with all the elements of an array, are EXACTLY the same - no matter what that 'something' is.

There really isn't much new inside the loop. We ask the user for the correct inventory amount for an item, taking advantage of the fact that 'itemCode' contains the code for the item under consideration. We then wait for the user to enter the value and, finally, store it in the correct element of the array.

We could even simplify this code and write:

for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
{

cout << "Please enter the inventory count for item: " << itemCode;
cin >> itemInventory[itemCode];
}

This code would have the same effect as the code above with the input going directly into the array element as opposed to going first to a local variable and then into the array element.

Similarly we could write:

for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
{

cout << "The inventory amount for item " << itemCode << " is: "
       << itemInventory[itemCode] << endl;
}

This will output the amount of each item in the inventory.

Note that as long as we have written the 'for' statement initialization and stopping conditions correctly, there is no reason to do any bounds checking here. The value for 'itemCode' cannot be less than 0 or greater than 9999.


Top of Section Main Menu Next Section