Two-dimensional arrays in C ++

двумерные массивы c++, двумерные массивы с++, многомерные массивы с++

In addition to one-dimensional arrays, you may need for the use of multi-dimensional array (two-dimensional, three-dimensional…). This lesson will be considered two-dimensional arrays. They are the most common, and the rest are extremely rare.

We have already discussed in previous articles dimensional arrays and C-strings (character arrays).It said, that array elements appear in memory sequentially – element by element. Visually, they can be represented as a single string in the data memory. To access any element of the array, sufficient to indicate the name and index item. The first difference is a two-dimensional array of one-dimensional – its elements comprise two indices: int arr [3][4]; Data of this array can be represented, a table: 3 х 4.

двумерные массивы c++, многомерные массивы c++

The first index of the array name – This is the index of the string, second – column index.

двумерные массивы c++, многомерные массивы c++

When you have looked at these pictures, It can be said about the two-dimensional array as – This is an array of, wherein each array element is also. int arr [3][4]; – is an array of 3 elements, each of which is an array of 4 elements.

These two-dimensional array and are arranged sequentially in memory, but by string. First string index 0 – cells from the 0th to 3rd, below the line with index 1 – cells from the 0th to 3rd …

What can store the elements of two-dimensional arrays? For example, You can store the number of parking spaces in the multi-storey car park (6 floors and on each 15 parking places). To do this, declare a two-dimensional array int floorsAndParkings[6][15]; and write in his cell number of seats on each floor. The two-dimensional array can store the C-string. For example: char someStr [3][256]; So we announced an array, that will keep 3 on strings 256 characters each.

Initialization of a two-dimensional array.

Write data to a two-dimensional array, you can declare it. Consider for example parking spaces. Let's say in the parking 2 floor 4 parking space on each. Declare an array and initialize it:

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

To initialize this looked more readable, arrange it so:

As you remember, According to the standard C + +11, sign = you can miss. Strings are initialized on the same principle:

How to display data in a two-dimensional array? You can go a long way and refer to each manual element:

двумерные массивы c++, многомерные массивы c++

The output of two-dimensional array of C-strings on the screen a little easier, Since we need only specify the array name and the index of the string. Further, the output stream cout alone will display all of the elements of the character array, until it finds ''

двумерные массивы c++, многомерные массивы c++

Good! And if we need to fill an array and display data int floorsAndParkings[20][100] or char someStr[50][256]? This can be a thankless job ten times to facilitate, using loops. More precisely nested loops.

Let's look at an example with parking. Show the user the parking scheme: floors and parking space. To book a place he should choose floor number and seat number. After booking – write value 0 in the appropriate cell, that would mean “the place is busy”.

We used for loop ,in strings 15 – 24, to write data to array and display them simultaneously on the screen. If present this two-dimensional array as a table – the outer loop for passes in the indices of strings – from the 0-s to 6-s. Nested loop – indexes on columns (the cells of table strings) – from 0-s to-9-s. In strings 32 – 82 is loop do while. His role in, to again and again to offer book your place for car, as long as required to the user. There are two nested loops do while. They implement a variety of floors and parking lot with protection against incorrect input values. Strings 57 – 81 comprise block if else , which the, in the case of a correct choice of the user displays a message about the successful reservation. If the place is occupied (cell contains a value 0) – notify, it offers a range of floor and repeat the site and displays the updated parking scheme, which marked reserved seats.

It works like this:

двумерные массивы c++, многомерные массивы c++


двумерные массивы c++, многомерные массивы c++

I recommend to watch video. Two-dimensional arrays with 13 minutes.

I propose to solve several tasks on the theme two-dimensional arrays.

Share links to articles from our site with your friends on the social network. social buttons. networks under Article. Thank you in advance!

Newsletter of programming:

Two-dimensional arrays in C ++
4.9 (97.5%) 8 votes

23 thoughts on “Two-dimensional arrays in C ++

  1. Why when checking the array we take away from the floor and parkingPlace unit?
    (floorsAndParkings[floor – 1][parkingPlace – 1])

    1. Because of the array of cells numbering begins with 0. For example: A man enters the room 7 - We need to check the box 6.

  2. Why do I got an error “error : name lookup of ‘f’ changed for ISO ‘for’ scoping [-fpermissive]”.

  3. How to change places rows or columns? You can take neposredsvenno column and change his place?

  4. Код программы про места на парковке абсолютно не рабочий. При компиляции Microsoft Visual Studio 2017 выдал куеву тучу ошибок .

Leave a Reply

Your email address will not be published. Required fields are marked *