In C++ an Array is a collection of elements of the same type, of fixed size stored in contiguous memory locations.
Each element of the array can be accessed using a numerical index, starting from zero. For this, the [] operator is used.
If you want to learn more, check out the Introduction to Programming Course
Array Declaration
The basic syntax for declaring an array in C++ is:
Type arrayName[size];
Where:
- Type: Is the data type the array will contain, such as
int,double,char, etc. - arrayName: Is the identifier of the array.
- size: Is the number of elements the array can hold.
For example, to declare an array of integers with 5 elements:
int myNumbers[5];
In this case, myNumbers is an array that can hold 5 integers.
Array Creation and Initialization
Once an array is declared, it can be initialized in several ways:
You can initialize an array with specific values at the moment of declaration:
int numbers[5] = {1, 2, 3, 4, 5};
In this example, the numbers array is initialized with the values 1, 2, 3, 4, and 5.
If you initialize an array with fewer elements than the declared size, the remaining elements will be initialized with default values (usually 0 for numeric types):
int numbers[5] = {1, 2}; // The array is initialized as {1, 2, 0, 0, 0}
C++ also allows automatic size initialization of the array if a list of values is provided:
int numbers[] = {1, 2, 3, 4, 5}; // The size of the array is automatically adjusted to 5
Using the Array
Accessing Elements
Each element of an array is accessed using an index, which starts at 0. The syntax to access an element is:
arrayName[index]
For example:
int numbers[] = {1, 2, 3, 4, 5};
int firstNumber = numbers[0]; // firstNumber will be 1
Modifying Elements
You can modify array elements by assigning new values to specific indices:
int numbers[] = {1, 2, 3, 4, 5};
numbers[2] = 10; // The third element of the array will now be 10
Common Array Operations
Array Length
To get the number of elements in an array, you can use the sizeof operator:
int numbers[] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]); // length will be 5
Sorting an Array
To sort arrays you can use the sort function from the standard <algorithm> library:
#include <algorithm>
int numbers[] = {5, 3, 1, 4, 2};
std::sort(numbers, numbers + 5); // Sorts the array in ascending order
Reversing an Array
To reverse the order of elements, you can also use the reverse function from <algorithm>:
#include <algorithm>
int numbers[] = {1, 2, 3, 4, 5};
std::reverse(numbers, numbers + 5); // Reverses the array
Multidimensional Arrays
Arrays can have more than one dimension, allowing data to be stored in more complex structures like matrices.
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
In this example, matrix is a two-dimensional array with 2 rows and 3 columns.
To access elements in multidimensional arrays you would do it like this,
std::cout << "Element in row 1, column 2: " << matrix[1][2] << std::endl; // Prints 6
Practical Examples
Calculate the average of an integer array
This example shows how to calculate the average of the elements in an integer array:
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
double average = static_cast<double>(sum) / 5;
std::cout << "The average is: " << average << std::endl;
return 0;
}
Find the maximum value in an array
This example shows how to find the maximum value in an integer array:
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int maximum = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > maximum) {
maximum = numbers[i];
}
}
std::cout << "The maximum value is: " << maximum << std::endl;
return 0;
}
Count how many elements are greater than a given value
This example counts how many elements in an array are greater than a specific value:
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int value = 3;
int count = 0;
for (int i = 0; i < 5; i++) {
if (numbers[i] > value) {
count++;
}
}
std::cout << "There are " << count << " elements greater than " << value << std::endl;
return 0;
}
Find the position of an element in an array
This example shows how to find the position of a specific element in an array:
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int search = 4;
int position = -1;
for (int i = 0; i < 5; i++) {
if (numbers[i] == search) {
position = i;
break;
}
}
if (position != -1) {
std::cout << "The number " << search << " is located at position " << position << std::endl;
} else {
std::cout << "The number " << search << " is not found in the array" << std::endl;
}
return 0;
}
