Free Hosting

Arrays (Part I)


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:
1
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.
1
2
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:
1
2
3
4
5
6
7
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:
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]; // 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:
1
2
3
4
5
6
7
8
9
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:
1
2
// 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.

0 comments:

Blogger Template by Clairvo