C++ Arrays

What Are Arrays?

Arrays in C++ allow programmers to store multiple values of the same data type within a single variable. They are useful for grouping related values, making it easier to work with and manipulate large sets of data.

To declare an array in C++, you specify the data type of the elements it will contain, followed by the array name and square brackets []. For example, to declare an array of integers named numbers, you would write:

int numbers[];

Arrays can be initialized at the time of declaration using a comma-separated list of values enclosed in curly braces {}. For example:

int numbers[] = {1, 2, 3, 4, 5};

This initializes an array with values from 1 to 5.

Accessing Array Elements

To access individual elements of an array, use square brackets [] and specify the index of the element. The first element of the array has an index of 0, the second element has an index of 1, and so on. For example:

numbers[0]

This would access the first element in the numbers array.

You can also modify elements by assigning new values to specific indices. For example:

numbers[2] = 10;

This assigns the value 10 to the third element in the numbers array.

Why Use Arrays in C++?

Arrays are essential for storing and manipulating data efficiently. They offer a convenient way to store multiple values of the same data type in a single variable. By using arrays, C++ programmers can handle large datasets and perform operations such as sorting, searching, or mathematical computations effectively.

Array Elements

To access array elements, use the array subscript operator []. For example, if you have an array called numbers that contains the values [1, 2, 3, 4, 5], you can access the second element using:

numbers[1]

For multidimensional arrays, you use multiple sets of brackets to access elements. For example, with a 2D array called matrix:

int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

To access the element 5, you would use:

matrix[1][1]

The first bracket specifies the row, and the second bracket specifies the column.

One-Dimensional Arrays

One-dimensional arrays store multiple elements of the same data type in a single, linear dimension. They allow for the sequential organization of a fixed number of elements.

The elements in a one-dimensional array are arranged in a contiguous memory block. Each element is identified by its index, starting from 0 and ending at n-1, where n is the total number of elements in the array.

To print all the elements of a one-dimensional array using a for loop:

for (int i = 0; i < 5; i++) {
    printf("%d ", numbers[i]);
}

Multidimensional Arrays

Multidimensional arrays in C++ organize data in a table-like structure with multiple dimensions. They are useful for representing data that naturally fits into more than one dimension, such as matrices or grids.

To declare a multidimensional array, specify the number of dimensions and the size of each. For example, a two-dimensional array:

int matrix[3][2] = {{1, 2}, {3, 4}, {5, 6}};

Here, matrix[1][1] would access the element with value 4.

Multidimensional arrays are commonly used to represent more complex data structures, such as 3D arrays for data involving height, width, and depth, commonly found in graphics programming.

Two-Dimensional Arrays

A two-dimensional array is a collection of elements arranged in rows and columns, like a grid. Each element is accessed using two indices: one for the row and one for the column. For example, a 3x3 two-dimensional array:

int array[3][3];

To print the elements:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        printf("%d ", array[i][j]);
    }
    printf("\n");
}

Two-dimensional arrays are useful for data that naturally forms a table or matrix, providing efficient access and manipulation through two indices.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate