Interested in learning the C++ programming language? Try doing it with One Hour a Day.
Using arrays lets you assemble multiple data into a single entity. Using a primitive data type variable like an int or a double only allows you to store a single value. But when you make that variable an array, then you can store more than one value in one single variable entity.
This article will only consider static arrays -- that is, their size is already pre-determined and cannot be changed once the array has been declared. If you are looking for arrays that can change their size after declaration, those are called dynamic arrays.
In C++, arrays are declared with the following syntax:
data_type arrayName[arraySize];
data_type arrayName[arraySize] = { firstVal, secVal, ..., lastVal };
First, you have to say the type of you data you will be using. That could be any primitive data type such as char, int, float, double or more complex data types such as objects.
After that, give the array a name and tell its size (or length) within the brackets. The size has to be a constant, something that has already been pre-determined. Additionally, you may also want to initialize the array with given values. You do that enclosing each value in a comma-separated-list, within braces (you have to match the number of items with the array size). If you initialize the array right away, you can omit the arraySize and make the compiler guess the size from the number of items in the list. Note that the values have to be all of the same data type because you are declaring an array of a specific type.
As a concrete example:
// Declares an array of integers with 8 elements
int myIntegerArray[8];
// Declares and initializes an array of doubles with 6 elements
double myDoubleArray[6] = { 2.43, 1.24, 1.7, 1.234, 1.9, 3.0 };
// Declares and initializes an array of characters with 3 elements
// (array size guessed from number of items in the comma-separated list)
char myCharArray[] = { 'a', 'b', 'c' };
Note the above array of integers was not initialized, so there is not any meaning value assigned to each element yet.
To go through the items of each array, you usually do a for loop:
for (int i = 0; i < arraySize; i++)
{
// goes through each array element
}
The loop starts at the first element -- which is always element "0" because array elements are counted from 0 and not one. The last array element is always the array size minus one, so we use just the greater-than operator; the loop stops after "i" becomes the same value as the array size.
As a concrete example:
// Goes through the array of integers (that has size 8) and
// sets each element to current value of i multiplied by 2
for (int i = 0; i < 8; i++)
{
myIntegerArray[i] = i * 2;
// Displays the current element value that was just assigned above
cout << myIntegerArray[i] << endl;
}
Output:
0
2
4
6
8
10
12
14
One thing to keep a watch on is that in C++, you might find yourself accidentaly trying to find an element that is out of bounds in an array. Say your array has 8 elements. The last element has index [7] because you start counting elements from [0]. So the last element would be referred to myArray[7] in this case. But what if you tried to access myArray[8]? Well, this element does not really exist -- you are going out of bounds! It is like you have gone into someone else's property. Your C++ compiler might not complain about that. It will just assume that you are a good person and have not tresspassed into anyone else's property. Watch for this, especially when you do a "for" loop. Always make sure the loop condition uses the right comparison operator (typically you would use <, but you could also use <=) to prevent you from accessing elements beyond the array size.
Let us now look at another example using the last two previously declared arrays:
// Store the array size in an integer variable
int myDoubleArraySize = 6;
for (int i = 0; i < myDoubleArraySize; i++)
{
cout << "myDoubleArray[" << i << "] has value " << myDoubleArray[i] << endl;
}
Output:
myDoubleArray[0] has value 2.43
myDoubleArray[1] has value 1.24
myDoubleArray[2] has value 1.7
myDoubleArray[3] has value 1.234
myDoubleArray[4] has value 1.9
myDoubleArray[5] has value 3
The above just goes through each element in the array of doubles and displays the corresponding value.
// Store the array size in an integer variable
int myCharArraySize = 3;
for (int i = 0; i <= (myCharArraySize - 1); i++)
{
cout << myCharArray[i] << endl;
}
Output:
a
b
c
The above goes through each of the three elements in the array of chars. Note how my loop condition is different from the previously used less-than only operator. Here, instead of using i < arraySize, I am using i <= arraySize MINUS 1. That is exactly the same thing as checking for "i" less than the array size. I placed parenthesis between the expression just for emphasis. The loop ends when we reach arraySize minus 1, which is exactly the last element in the array.
Arrays are great when you have large amounts of data and you want to keep them organized and encapsulated into a single variable entity. When you work with arrays, always start counting from 0; the last array element is located at the size of array minus one. Arrays work well with loops because you can handle data conveniently by just using a single statement to take care of each and all of the array elements. Typically, you would use a for loop to go through the elements of an array, either for assignment or just to display their value.
Learning the C++ programming language? How about doing it with One Hour a Day.