| Section II: Accessing Array Elements | Section I : Introducing Arrays | Section III: Arrays and For Loops | Section IV: Arrays as Parameters |
|
A reference document on the basic elements of C++.
|
A. Naming the Individual Elements At the code level, the C++ name for the memory location for the first
item in the inventory array is:
while the name for the 10th item is:
Note that although we have 10,000 items, there is no
"itemInventory[10000]" As a matter of fact, to write
"itemInventory[10000]" in the program we are working on would likely cause
serious problems because we would be accessing the first memory location
past the end of the array. Thus, we declare our array using the
value 10,000, meaning that there are 10,000 integer memory locations to
set aside BUT we use subscript values in the range 0 to 9999. At this point we know that each individual array element is a unique
memory location and that the array name along with a subscript enclosed in
brackets represents that memory location. Further, each individual array
element is of some type - the type included in the array declaration. In
other words, when one writes "itemInventory[3456]", one is referring to a
specific integer. Therefore, "itemInventory[3456]" can be used ANYWHERE in
a program that an integer can be used. The same is true for array elements
of any type. We will see later, for example, that if we declare an array
to hold elements of some class type, the elements of that array can be
used anywhere instances of the class can be used. B. Working with the Individual Elements Suppose we wanted to state that there are 20 items in the inventory
with item code 4. We could write this as:
The code is the same as any other assignment statement, except for the
subscript. Or, suppose we wanted to add 1 to the first inventory item. We could
now write this as:
If we wanted to read in a value and store that value in the location
holding the amount for item code 22, we could write:
And, if later we wished to output the inventory count for that same
item, we could write:
We can also use these elements in arithmetic expressions. Suppose, for
example, that we wanted to find out the worth of our inventory for the
item with item code 12. Remembering that we also have an array of item
prices called 'itemPrices', we could calculate this using the code:
Likewise, we can use array elements in relational expressions. For
example if would be legal to write:
This would mean that "some code" would be executed if there were fewer
than 2,345 items with the inventory code '342'. To repeat, since the array, 'itemInventory', holds integers, we can use
the individual elements of that array ANYWHERE integer elements can be
used. And, the same can be said for any other array type. One thing that has not been said yet is that we cannot in general use
JUST the name of the array. It makes no sense, for example, to write:
or
Which inventory item gets the value 22? Or, which inventory item is to
be output? We will see in a bit how to quickly output, for example, all
the elements of an array C. Variables as Indices
Remembering that item codes now start at 0 instead of 1 and that the
item code for item 9999 would now be 9998, we can rewrite this as:
This by itself does not gain us much. However, there is no reason why
our subscript values must be constants. It is perfectly legal for
subscripts to be variables or even expressions (for example, var1 + var2)
- as long as those expressions evaluate to integers. Subscripts must
evaluate to integers because it is, of course, impossible to talk about
the 2.5th element of an array. Using the previous declaration for itemInventory and assuming that the
individual items have been loaded with the correct inventory values, it
would be perfectly legal to write the code:
The key here is the last line. Since 'itemCode' has the value
9998, this code is the equivalent of
Now we go one step further and ask the user for the item code. (In a
store this information would be received from the device that reads bar
codes at the check-out counter.).
The last line still has the same effect, but instead of the value of
'itemCode' being hard coded into the program, it is entered by the user.
Since there are no longer 10,000 independent variables, these few lines
can take the place of that monster 'switch' statement. And, if we put this
code inside a while loop, we could handle purchases all day long. There is one potential problem that we should take
care of here. What if the user enters a negative number for the item code
or a number greater than 9999. We would then be attempting to access and
change memory that is NOT part of the array. C++ does not automatically
perform what is called bounds checking - checking to see if an
array index falls into a valid range. We must do this ourselves by not
allowing the access to take place if the code is out of range:
|
|