We will need to use arrays in elementary ways in order to have interesting examples to illustrate other concepts. This discussion leaves out a lot. We will look at arrays in more detail later.
Arrays
Indices start at 0
The indices of an array always start at 0. For example, an array A of 5 items has indices 0, 1, 2, 3 and 4, so A[0], A[1], A[2], A[3] and A[4] are the variables in A. |
When carrying out hand simulations with arrays, it is common practice to draw an array as a table of boxes, with the array name above the table, and to number the boxes. For example, an array called B of 4 integers is diagrammed as follows.
B | |
0 | |
1 | |
2 | |
3 |
Notice that the variables in the array are shown empty. They are uninitialized when the array is created. Performing statements
B[0] = 2; B[1] = B[0] + 2;changes array B as follows.
B | |
0 | 2 |
1 | 4 |
2 | |
3 |
You are required to create an array before you use it.
All of the variables in an array must have the same type. For example, you can have an array of ints, an array of doubles, etc.
C++ differs from Java in what array creation looks like. Statement
int A[10];declares an array of ints named A. In general, the form is
T name[size];where T is the type of each variable in the array, name is the array's name and size is the number of variables. For example,
double stuff[100];creates an array called stuff of size 100, where each variable in array stuff has type double.
You cannot ask an array how large it is
C++ arrays have an important difference from Java arrays. In Java, you write a.size to get the size of array a. There is no corresponding thing in C++. You cannot ask an array how large it is. We will see ways of getting around that later. For now, just remember that there is nothing that you can write to get the size of an array. The array does not know how large it is. |
Array size is fixed when the array is created
The size of an array is determined when you create the array, and cannot be changed. For example, after int n = 5; int jump[n]; n = n + 1;array jump still has size 5. In the second line, array jump is created using the current value of n. Changing n only changes n, not array jump. |
Use named constants for arrays with fixed sizes
There is a general rule about software design called
the single point of modification rule.
Occasionally you need to create an array before you know how large it needs to be. In that case, you will need to choose a reasonable size. The size that you choose is an arbitrary decision, so it should be changeable by changing one line. Use a named constant for the array size. For example, you can create a global constant, written outside of any function. Global constants do not have the same restrictions as global variables. You can create global constants without permission. Declaration const maxNumInputs = 50;says that maxNumInputs is 50. When you need to create an array that holds inputs, write: int inputs[maxNumInputs];The standards require you to do that. Make a separate constant for each arbitrary decision that you make. Do not have one constant, MAX, that you use for all array sizes. You don't want to force all decisions to be the same. The standards require you to do that. |
An array is a collection of indexed variables, all of the same type.
If A is an array then A[i] is the variable in array A at index i. The indices go from 0 to n−1, where n is the size of A.
Statement
int B[n];creates an array called B of size n.
Either compute the size of an array or, if necessary, use a named constant for the size.
Write a statement that defines a constant called maxNumKangaroos, with value 200. Write another statement that creates an array integers called kangaroo of size maxNumKangaroos. Answer
Using the array kangaroo from the preceding question, write a statement that sets the first variable in array kangaroo to 7. Answer
Using array kangaroo from question 1, does the following make sense?
kangaroo[maxNumKangaroos] = -1;Answer