Language: EN

cpp-que-son-arrays

What are arrays and how to use them in C++

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 through a numeric index, starting from zero. For this, the [] operator is used.

Declaration of Arrays

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 hold 5 integers.

Creation and initialization of arrays

Once an array is declared, it can be initialized in several ways:

Initialization with default values

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.

Partial initialization

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}

Automatic initialization

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 through 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

Length of the Array

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

Practical examples

Calculating 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;
}

Finding 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;
}

Counting 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 counter = 0;

    for (int i = 0; i < 5; i++) {
        if (numbers[i] > value) {
            counter++;
        }
    }

    std::cout << "There are " << counter << " elements greater than " << value << std::endl;

    return 0;
}

Finding 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;
}