Monday, January 17, 2011

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!
1int nTestScoreStudent1;
2int nTestScoreStudent2;
3int nTestScoreStudent3;
4// ...
5int nTestScoreStudent30;
Arrays give us a much easier way to do this:
1int 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.
1int anArray[5]; // allocate 5 integers
2anArray[0] = 7; // put the value 7 in element 0
Let’s take a look at a simple program that uses arrays:
1int anArray[3]; // allocate 3 integers
2anArray[0] = 2;
3anArray[1] = 3;
4anArray[2] = 4;
5
6int nSum = anArray[0] + anArray[1] + anArray[2];
7cout << "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:
1int anArray[5];
2int nIndex = 3;
3anArray[nIndex] = 7;
However, when doing array declarations, the size of the array must be a constant.
01int anArray[5]; // Ok -- 5 is a literal constant
02
03#define ARRAY_SIZE 5
04int anArray[ARRAY_SIZE]; // Ok -- ARRAY_SIZE is a symbolic constant
05
06const int nArraySize = 5;
07int anArray[nArraySize]; // Ok -- nArraySize is a variable constant
08
09enum ArrayElements
10{
11    MAX_ARRAY_SIZE = 5;
12};
13int anArray[MAX_ARRAY_SIZE]; // Ok -- MAX_ARRAY_SIZE is an enum constant
14
15int nSize = 5;
16int 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:
1double adArray[5]; // declare an array of 5 doubles
2adArray[2] = 7.0; // assign 7.0 to array element 2
3
4struct sRectangle
5{
6    int nLength;
7    int nWidth;
8};
9sRectangle 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// sets the nLength member of array element 0
2asArray[0].nLength = 24;
Elements of an array are treated just like normal variables, and as such have all of the same properties.

No comments: