Free Hosting

Arrays (Part II)


Initializing Arrays
Because array variables are treated just like normal variables, they are not initialized when created. C++ provides a convenient way to initialize entire arrays via use of an initializer list.
1
2
3
4
5
6
int anArray[5] = { 3, 2, 7, 5, 8 };
cout << anArray[0] << endl;
cout << anArray[1] << endl;
cout << anArray[2] << endl;
cout << anArray[3] << endl;
cout << anArray[4] << endl;
Which prints:
3
2
7
5
8
What happens if you don’t initialize all of the elements in an array? The remaining elements are initialized to 0:
1
2
3
4
5
6
int anArray[5] = { 3, 2, 7 };
cout << anArray[0] << endl;
cout << anArray[1] << endl;
cout << anArray[2] << endl;
cout << anArray[3] << endl;
cout << anArray[4] << endl;
Which prints:
3
2
7
0
0
Consequently, to initialize all the elements of an array to 0, you can do this:
1
2
// Initialize all elements to 0
int anArray[5] = { 0 };
Omitted Size
If you are initializing an array of elements using an initializer list, the compiler can figure out the size of the array for you, and you can omit explicitly declaring the size of the array:
1
int anArray[] = { 0, 1, 2, 3, 4 }; // declare array of 5 elements
Sizeof
The sizeof operator can be used with arrays. It returns the total size allocated for the entire array:
1
2
int anArray[] = { 0, 1, 2, 3, 4 }; // declare array of 5 elements
cout << sizeof(anArray); // prints 20 (5 elements * 4 bytes each)
In C++, there is no direct way to ask an array how many elements it contains. However, using the sizeof operator, we can figure it out:
1
int nElements = sizeof(anArray) / sizeof(anArray[0]);
Because all of the elements of the array have the same size, dividing the total size of the array by the size of any one of the elements yields the number of elements in the array! We use element 0 because it is the only element guaranteed to exist, as arrays must have at least one element.
Arrays and Enums
One of the big documentation problems with arrays is that that integer indices do not provide any information to the programmer about the meaning of the variable. Consider a class of 5 students:
1
2
3
const int nNumberOfStudents = 5;
int anTestScores[nNumberOfStudents];
anTestScores[2] = 76;
Who is represented by array element 2? It’s not clear. Consequently, when known in advance, it is common to use enumerated values to index the array:
1
2
3
4
5
6
7
8
9
10
11
12
enum StudentNames
{
    KENNY, // 0
    KYLE, // 1
    STAN, // 2
    BUTTERS, // 3
    CARTMAN, // 4
    MAX_STUDENTS // 5
};
 
int anTestScores[MAX_STUDENTS]; // allocate 5 integers
anTestScores[STAN] = 76;
In this way, it’s much clearer what each of the array elements represents. Note that an extra enumerator named MAX_STUDENTS has been added. This enumerator is used during the array declaration to allocate one slot for each enum. This is useful for documentation purposes, and because the array will automatically be resized if another enumerator is added:
1
2
3
4
5
6
7
8
9
10
11
12
13
enum StudentNames
{
    KENNY, // 0
    KYLE, // 1
    STAN, // 2
    BUTTERS, // 3
    CARTMAN, // 4
    WENDY, // 5
    MAX_STUDENTS // 6
};
 
int anTestScores[MAX_STUDENTS]; // allocate 6 integers
anTestScores[STAN] = 76;
Note that this “trick” only works if you do not change the enumerator values manually!
Quiz
1) Declare an array to hold the high temperature (to the nearest tenth of a degree) for each day of a year. Assign a value of 0 to each day.
2) Set up an enum with the names of the following animals: chicken, dog, cat, elephant, duck, and snake. Allocate an array with an element for each of these animals, and use an initializer list to initialize each element to hold the number of legs that animal has.

0 comments:

Blogger Template by Clairvo