In a previous lesson, you learned that you can use structs to aggregate many different data types into one variable. However, structs are not the only aggregate data type. An array is an aggregate data type that lets you access multiple variables through a single name by use of an index. In C++, all of these variables must have the same type.
Consider the case where you want to record the test scores for 30 students in a class. To do so, you would have to allocate 30 variables!
int nTestScoreStudent1; int nTestScoreStudent2; int nTestScoreStudent3; // ... int nTestScoreStudent30;
Arrays give us a much easier way to do this:
int anTestScores[30]; // allocate 30 integers
In the above example, we declare an array named anTestScores. When used in an array definition, the subscript operator ([]) is used to tell the compiler how many variables to allocate. In this case, we’re allocating 30 integers. Each of these variables in an array is called an element.
To access each of our 30 integer array elements, we use the subscript operator with an integer parameter called an index to tell the compiler which variable we want. The first element of our array is named anTestScores[0]. The second is anTestScores[1]. The tenth is anTestScores[9]. Note that in C++, arrays always count starting from zero! This means an array of size N has array elements 0 through N-1. This can be awkward for new programmers who are used to counting starting at 1.
Note that the subscript operator actually has two uses here: in the variable declaration, the subscript tells how many elements to allocate. When using the array, the subscript tells which array element to access.
int anArray[5]; // allocate 5 integers anArray[0] = 7; // put the value 7 in element 0
Let’s take a look at a simple program that uses arrays:
int anArray[3]; // allocate 3 integers anArray[0] = 2; anArray[1] = 3; anArray[2] = 4; int nSum = anArray[0] + anArray[1] + anArray[2]; cout << "The sum is " << nSum << endl;
This program produces the result:
The sum is 9
Array elements may be accessed by a non-constant integer variable:
int anArray[5]; int nIndex = 3; anArray[nIndex] = 7;
However, when doing array declarations, the size of the array must be a constant.
int anArray[5]; // Ok — 5 is a literal constant
#define ARRAY_SIZE 5
int anArray[ARRAY_SIZE]; // Ok — ARRAY_SIZE is a symbolic constant
const int nArraySize = 5;
int anArray[nArraySize]; // Ok — nArraySize is a variable constant
enum ArrayElements
{
MAX_ARRAY_SIZE = 5;
};
int anArray[MAX_ARRAY_SIZE]; // Ok — MAX_ARRAY_SIZE is an enum constant
int nSize = 5;
int anArray[nSize]; // Not ok! — nSize is not a constant!
To summarize, array elements can be indexed with constants or non-constants, but arrays must be declared using constants. This means that the array’s size must be known at compile time!
Arrays can hold any data type, including floating point values and even structs:
double adArray[5]; // declare an array of 5 doubles
adArray[2] = 7.0; // assign 7.0 to array element 2
struct sRectangle
{
int nLength;
int nWidth;
};
sRectangle asArray[5]; // declare an array of 5 sRectangle
To access a struct member of an array element, first pick which array element you want, and then use the member selection operator to select the member you want:
// sets the nLength member of array element 0 asArray[0].nLength = 24;
Elements of an array are treated just like normal variables, and as such have all of the same properties.
6.2 — Arrays (Part II)
|
Index
|
5.8 — Break and continue
|
6.2 — Arrays (Part II)
Index
5.8 — Break and continue
Alex, not sure what is meant in this example, where array elements may be accessed by a non-constant integer variable; is “nIndex” the assigned index value, or is it an element value? I understand the first line and the last line seems to indicate that the value of 7 is being put into element nIndex. If so, should nIndex on line 2 be in [ ]?
nIndex is the assigned index value. We set the value of nIndex on line 2 of your example. The element value is what comes after the assignment on the third line.
To be as clear as possible: array indices must be integers. You can use either a literal index:
or a variable index:
nIndex doesn’t have to be a constant integer.
Both of the above examples do the exact same thing: assign the value of 7 to array element 3. When the statement anArray[nIndex] = 7 is evaluated, the array index part takes precedence and is evaluated first. nIndex is evaluated to produce the value 3, and then anArray[3] = 7 assigns the value 7 to array element 3.
The index itself is just an integer — thus, no need for []. You only need the [] when you’re accessing the array itself.
Thanks, it makes more sense now.
thanks Alex
but I still get some confuse .. could you please give an example like a complete program about what we could do with array ..
thanks
thanks Alex!
Would you tell me about array with unknown number elements?
thank you so much!
I’m not sure I understand your question. What do you mean by an unknown number of elements?
It sounds to me like you’re talking about arrays where the size isn’t known in advance — this is handled via dynamic allocation, and is covered in lesson 6.9.
int anTestScores[30]; // allocate 30 integers || anArray[3]; // allocate 3 integers
If it starts counting at 0, shouldn’t these instead read anTestScores[29] and anArray[2] ?