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 about Arrays in C++
check the Introduction to Programming Course read more
Array declaration
The basic syntax for declaring an array in C++ is:
Type arrayName[size];
Where:
- Type: Is the data type that the array will contain, such as
int
,double
,char
, etc. - arrayName: Is the identifier of the array.
- size: Is the number of elements that the array can contain.
For example, to declare an array of integers with 5 elements:
int myNumbers[5];
In this case, myNumbers
is an array that can contain 5 integers.
Creating and initializing arrays
Once an array is declared, it can be initialized in several ways:
You can initialize an array with specific values at the time of declaration:
int numbers[5] = {1, 2, 3, 4, 5};
In this example, the array numbers
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 (generally 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 the elements of the array 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 operations with arrays
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 library <algorithm>
:
#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 the 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 for the storage of data in more complex structures like matrices.
Example of a two-dimensional array
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.
Accessing elements in multidimensional arrays
std::cout << "Element in row 1, column 2: " << matrix[1][2] << std::endl; // Prints 6
Practical examples
Calculate the average of an array of integers
This example shows how to calculate the average of the elements in an array of integers:
#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 array of integers:
#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;
}
:::::::