C++ Array
C++ supports array data structures, which can store a fixed-size sequential collection of elements of the same type. Arrays are used to store a series of data, but they are often considered as a series of variables of the same type.
The declaration of an array does not involve declaring individual variables like number0, number1, ..., number99, but rather declaring an array variable, such as numbers, and then using numbers[0], numbers[1], ..., numbers[99] to represent individual variables. Specific elements in the array can be accessed via their index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.
Declaring an Array
To declare an array in C++, you need to specify the type of elements and the number of elements, as shown below:
type arrayName [ arraySize ];
This is called a one-dimensional array. arraySize must be an integer constant greater than zero, and type can be any valid C++ data type. For example, to declare an array balance of type double with 10 elements, the declaration statement is as follows:
double balance[10];
Now balance is an available array that can hold 10 numbers of type double.
Initializing an Array
In C++, you can initialize an array individually or using an initialization statement, as shown below:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements specified in the square brackets [ ].
If you omit the size of the array, the size of the array will be the number of elements in the initialization. Thus, if:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create an array that is identical to the one created in the previous example. Here is an example of assigning a value to a specific element in the array:
balance[4] = 50.0;
The above statement assigns the value 50.0 to the fifth element of the array. All arrays have 0 as the index of their first element, also known as the base index, and the last index of an array is the total size of the array minus one. The following is a graphical representation of the array discussed above:
Accessing Array Elements
Array elements can be accessed by their array name plus index. The index of the element is placed within square brackets following the array name. For example:
double salary = balance[9];
The above statement will assign the value of the 10th element of the array to the salary variable. The following example uses the three concepts mentioned above: array declaration, array assignment, and array access:
Example
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main ()
{
int n[ 10 ]; // n is an array of 10 integers
// Initialize array elements
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100; // Set element i to i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// Output the value of each element
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
The above program uses ** to format the output. When the above code is compiled and executed, it produces the following result:
Element Value
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
Detailed Explanation of Arrays in C++
In C++, arrays are very important, and we need to know more details about arrays. The following are some important concepts related to arrays that C++ programmers must be aware of:
Concept | Description |
---|---|
Multidimensional Arrays | C++ supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array. |
Pointer to an Array | You can generate a pointer to the first element of an array by specifying the array name without an index. |
Passing Arrays to Functions | You can pass a pointer to an array to a function by specifying the array name without an index. |
Returning Arrays from Functions | C++ allows arrays to be returned from functions. |