CHAPTER 7
| 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 A reference document on the basic elements of C++.
3. The Patterns
|
A. Initializing an Array
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 Pattern Name:
Purpose:
Pattern:
int itemAmount;
for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
{
cout << "Please enter the inventory count for item: " << itemCode;
cin >> itemAmount;
itemInventory[itemCode] = itemAmount;
}
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++)
} 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++) } 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.
| |