<< Chapter < Page | Chapter >> Page > |
int ages[] = {49,48,26,19,16};int counter;
for (counter = 0, counter<5, counter++)
{cout<<ages[counter]<<endl;
}
This second usage of the square brackets is as the array notation of dereference or more commonly called the index operator . As an operator it provides the value held by the member of the array. For example, during one of the iterations of the for loop the index (which is an integer data type) will have the value of 3. The expression ages[counter] would in essence be: ages[3]. The dereference operator of [3] means to go the 3 rd offset from the front of the ages array and get the value stored there. In this case the value would be 19. The array members (or elements) are referenced starting at zero. The more common way for people to reference a list is by starting with one. Many programming languages reference array members starting at one, however for some languages (and C++ is one of them) you will need to change your thinking . Consider:
Position | C++ | Miss America | Other Contests |
zero offsets from the front | ages [0] | Winner | 1 st Place |
one offsets from the front | ages [1] | 1 st Runner Up | 2 nd Place |
two offsets from the front | ages [2] | 2 nd Runner Up | 3 rd Place |
three offsets from the front | ages [3] | 3 rd Runner Up | 4 th Place |
four offsets from the front | ages [4] | 4 th Runner Up | 5 th Place |
Saying that my cousin is the 2 nd Runner Up in the Miss America contest sounds so much better than saying that she was in 3 rd Place. We would be talking about the same position in the array of the five finalists.
Rather than using the for loop to display the members of the array, we could have written five lines of code as follows:
cout<<ages[0]<<endl;
cout<<ages[1]<<endl;
cout<<ages[2]<<endl;
cout<<ages[3]<<endl;
cout<<ages[4]<<endl;
int ages[] = {49,48,26,19,16};int counter;
for (counter = 0, counter<sizeof ages / sizeof ages[0], counter++){
cout<<ages[counter]<<endl;
}
Within the control of the for loop for the displaying of the grades, note that we calculated the number of the members in the array by using the sizeof operator. The expression is:
sizeof ages / sizeof ages[0]
When you ask for the sizeof an array identifier name the answer is how many total bytes long is the array (or in other words – how many bytes of storage does this array need to store its values). This will depend on the data type of the array and the number of elements. When you ask for the sizeof one of its members, it tells you how many bytes one member needs. By dividing the total number of bytes by the size of one member, we get the answer we want: the number of members in the array. This method allows for flexible coding . By writing the for loop in this fashion, we can change the declaration of the array by adding or subtracting members and we don't need to change our for loop code.
Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials.
Download from Connexions: Demo_Arrays.cpp
Notification Switch
Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?