Arrays
An array is a structured type which provides a collection of data items, all of the same type, that can be accessed individually. In Java, arrays are dynamic objects containing a sequential collection of elements and a length field as illustrated below In C++, an array is a sequential collection of elements but they can be either static or dynamic. That is, their size can be specified at compile-time or run-time. A few notes about arrays in C++
Static ArraysA static array is one in which the size is specified at compile-time. Thus, the size of the array must be a literal integer value or a value stored in a constant literal variable. Creating a Static Array. To create a static array, the data type and the number of elements contained in the array are specified int gradeList[ 5 ]; Note that the elements themselves are not initialized to any given value and will contain whatever value was previously in that location. Assuming values have been assigned to the array, the result of the above declaration appears as follows Even though the array Array Size. Since C++ does not keep track of the size of an array, it is easy to go out of bounds. You should create constant variables to represent the size of any array you create. That way, the constant variable can be used later in the program when you need to refer to the array size. const int NUM_GRADES = 5; int main() { int gradeList[ NUM_GRADES ]; : : int i = 0; while( i < NUM_GRADES ) { printf( "%d\n", gradeList[ i ] ); i++; } } Initializing Elements. A static array can be initialized when it is created using curly braces as in Java. Consider the following C++ examples int valueList[] = { 5, 10, 15, 20, 25, 30, 35 }; int bigList[ 10 ] = { 1, 2, 3 }; The static array In C++, you can initialize every element of a static array to a single value by including a single value within curly braces as in the following int smallList[ 4 ] = { 0 }; which results in Dynamic ElementsIn the example in the previous section, the static arrays contained static elements -- each element of the arrays were the same as a static variable of the given type. But arrays in C++ can also contain dynamic elements. That is, the elements are pointers to a given type. In the following example, int * oreValues[ NUM_VALUES ] = { NULL }; we create a 5 element static array in which each element is a pointer to an
Filling In The Array. If an array stores pointers to data, then the elements can only be assigned a pointer or reference. Consider the following code segment for( int i = 0; i < NUM_VALUES; i++ ) { moreValues[ i ] = new int; // assign a dyanmic variable. *moreValues[ i ] = i; // dereference the dynamic variable. } which produces the following Attach:cpparrayptrvalues.png Δ Dynamic Element Access. Since the elements of the array are pointers, we must use the dereferencing notation in order to access an individual element. The variable reference moreValues[ 2 ] refers to the third element of the array which is a pointer to an *moreValues[ 2 ] Array ParametersWhen an array is passed to a function, only the reference or pointer is passed and not the entire array. The C++ notation for passing an array uses empty brackets following the variable name (see the Arrays And Pointers section for an alternate method of passing arrays as parameters). void printGrades( int theGrades[], int num ) { for( int i = 0; i < num; i++ ) printf( "%d\n", theGrades[ i ] ); } To pass the array to the function, only the array name is specified since it is an printGrades( gradeList, NUM_GRADES ); The result of passing an array to a function is the assignment of the array or pointer variable to a local variable within the function. Remember that pointer asignment can only occur between pointers of the same type.
Arrays of Structsstruct StudentRecord { int idNum; int classYear; float gpa; }; const int MAX_RECORDS = 100; StudentRecord studentList[ MAX_RECORDS ]; int numRecords = 0; float computeAvgGPA( StudentRecord theList[], int numRecs ) { int sum = 0; for( int i = 0; i < numRecs; i++ ) sum = sum + theList[ i ].gpa; return float( sum ) / numRecs; }
|