Contents:

Learn Python without spending money ➞ A short course for beginners and experienced programmers. Four interesting projects in your portfolio and the opportunity to communicate with the teacher. Find out what awaits you in the course.
Learn moreThis article is the seventh in a series of publications called "Deep Dive into C++". In the previous article, we looked at how to use while, do-while, and for loops to simplify code. Now, we're focusing on arrays.
An array is a collection of memory cells arranged sequentially. It is used to conveniently store homogeneous information, such as employee salaries, game character coordinates, student grades, and other similar data.

The image above shows the declaration of an array consisting of four elements of the integer type. Even though the elements haven't been assigned values yet, the array will still occupy the equivalent of four variables. In this particular case, the size will be 16 bytes.
Arrays are a very efficient and fast tool, since the sequential arrangement of elements helps speed up the processing of information within them.
Methods for Creating Arrays in C++
There are several methods for creating arrays:
Array indexing starts from zero, not one. The array length is preserved in the usual format. For example, if an array consists of ten elements, the index of the last element will be 9.
Please note! Arrays are immutable, meaning they cannot be modified. You can change the values of individual elements, but the array itself remains immutable—its length cannot be changed, and you cannot assign the values of one array to another.
It is important to constantly monitor access to array elements to ensure you do not exceed its bounds. For example, if the array length is 5, accessing elements with indices 5, 6, 7, and so on can lead to unpredictable consequences.
This code will produce the following result:

Thus, the result will vary with each new access, since at the moment this memory cell is capable of contain absolutely any information.
Efficient Methods for Working with Arrays in C++
To access any element of an array, you need to use its index. This method is well suited if you need to extract one or two values. However, when it becomes necessary to iterate over the entire array, it is more efficient to use loops:
In this context, each cell of the array is manipulated, with the iterator (the i variable) acting as an index.

Loops also provide the ability to search within an array. For example, you can use them to determine the minimum or maximum value.
Here's what's shown on the display:

You can also use multiple arrays to store related data. For example, let's create an array named names, which contains the names of employees, and an array named salaries, which will contain their salaries.
Here is the result:

It is possible to use a simplified form of writing a for loop:
In this example, the nums variable is an array, and number is an identifier that allows you to access the current element of this array. For example, if we are on the second iteration of the loop, then when we print the value of number, we will see the element nums[1].
You can also select the auto data type, which will allow the program to independently determine which array to process.
Working with multidimensional arrays in C++
Multidimensional arrays are structures in which one array is contained within another. For example, a two-dimensional array is often referred to as a matrix.

The most obvious example of a matrix is a two-dimensional map. In this case, the rows correspond to the X-axis, while the columns represent the Y-axis. Using such an array, you can create a map for the game.
The result is a fairly basic game:

You can also form arrays with a higher number of dimensions, including three-dimensional, four-dimensional, and so on. However, working with such structures can be difficult, so it's best to limit their use to situations where it's truly necessary. For example, this can be relevant when creating 3D graphics.
Conclusion
Arrays are widely used in programming, whether in game development or machine learning. Therefore, learning how to work with them is essential. In the following discussion, we'll cover ready-made tools that can significantly simplify this task.
Read also:
- C++ in 2021: Applications, Future, and Key Knowledge to Get Started
C++ continues to be a popular programming language across a variety of fields. It is widely used in the development of system software, drivers, embedded systems applications, as well as in the creation of games and graphics applications. Its performance and ability to work with low-level components make it especially attractive for developers working with high-load systems.
The outlook for C++ remains positive due to its widespread use in high-tech areas such as artificial intelligence, machine learning, and financial technology. The language continues to evolve, and new standards such as C++20 add modern features that simplify writing code and improve its security.
For those looking to get started with C++, it is important to master the basics of the language syntax, as well as understand the concepts of object-oriented programming. Knowledge of the C++ standard library and the ability to use popular frameworks and development tools will also be useful. Beginner programmers should pay attention to practical projects and tasks, which will help consolidate theoretical knowledge and develop programming skills.
- First measure, then cut seven times: typical mistakes of experienced IT experts.
- Starting in Python programming: a quick guide.

