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!
1
2
3
4
5
|
int nTestScoreStudent1;
int nTestScoreStudent2;
int nTestScoreStudent3;
int nTestScoreStudent30;
|
Arrays give us a much easier way to do this:
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.
1
2
|
int anArray[5];
anArray[0] = 7;
|
Let’s take a look at a simple program that uses arrays:
1
2
3
4
5
6
7
|
int anArray[3];
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:
1
2
3
|
int anArray[5];
int nIndex = 3;
anArray[nIndex] = 7;
|
However, when doing array declarations, the size of the array must be a constant.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
int anArray[5];
#define ARRAY_SIZE 5
int anArray[ARRAY_SIZE];
const int nArraySize = 5;
int anArray[nArraySize];
enum ArrayElements
{
MAX_ARRAY_SIZE = 5;
};
int anArray[MAX_ARRAY_SIZE];
int nSize = 5;
int anArray[nSize];
|
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:
1
2
3
4
5
6
7
8
9
|
double adArray[5];
adArray[2] = 7.0;
struct sRectangle
{
int nLength;
int nWidth;
};
sRectangle asArray[5];
|
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:
Elements of an array are treated just like normal variables, and as such have all of the same properties.
0 comments: