The basics of programming in c++ for beginners

Two-dimensional arrays in 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 articlesdimensional 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 element as an array. 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 dataint 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++

continued…

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

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

I recommend to watch the video – Two-dimensional arrays

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

  1. Dear author of the article and site, if you are one person…The article about two-dimensional arrays is disgusting!!! Instead of showing how to populate a two-dimensional array correctly – explain, what you need to use attachments for loop in the same for loop and how to display a two-dimensional array on the screen, again using nested loops, or similar necessary things!!!, you fucking useless code on anyone 80 strings!!! You have a website for beginners!!?? And after that you give tasks about it…. In general, terribly stated!

    1. Great article! This is just a classic.. Test example. And arrays, and input-output and logic and array traversal, all in one. You antidot when you grow up, understand what i mean.

    2. And if you have antidot from such an example, your head is already boiling :), This is not good.

  2. Dear author, explain, you are welcome, what “” , “\n … or where you can read about this.

      1. Noticed a bug, when you come in with VK your avatar will be in place of ALL avatars, not only his.

  3. As a newbie, I just can’t understand this: floorsAndParkings[floor – 1][parkingPlace – 1] != 0 How an array can beat is equal to any one value.
    After all, the array is filled with many elements, which are written in braces. Explain please, I can't.

    1. Sergei, floorsAndParkings[floor – 1][parkingPlace – 1] != 0 this is not the whole array, and its separate specific cell. For example: user enters floor number 2 (floor ) and parking number 3 (parkingPlace). We remember, that the numbering of the array cells starts from zero, not from unit – therefore we subtract from the entered values ​​one by one. Get the specific floorsAndParkings cell[1][2] and check its value for inequality to zero. If not zero, then the place is free…

  4. I wonder how to provide the ability to input elements of a multidimensional array to the user, not in the process of writing a program?

    1. Andrei, we are now preparing a task with a solution. There will be implemented the Matrix class. The task will show, how to make input of a two-dimensional array from the keyboard. That is, the user will enter numbers. Enter = end of line, Enter twice = end of matrix input.

  5. Govnokod. and also wash the lies!!!!! I’ll conduct some research now. But first, I think a two-dimensional array is not stored in memory (and pointers to the beginning of one-dimensional arrays) Need to check.

    1. still yes, stored as a two-dimensional array (as in the article). I consider this a bug with ++

    2. before, than repeating words like “gondokod”, heard from pondering morons, which you yourself do not understand – you yourself would first deal with the most basic concepts … not even C ++, and coming from C, which are described 40 years ago (this is the case, when it says: read materiel).

  6. Why, at repeated deductions of the table, when the place is taken, in a nested for loop 75-78, do not write this line(20): floorsAndParkings[f][p] = p + 1; ???

Leave a Reply

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