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. 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. The program code is about the parking is absolutely not working. When you compile a Microsoft Visual Studio 2017 issued Cueva cloud errors .

  5. already some time everything that I write so interrupted TT may suggest why the function fails?
    #include
    using namespace std;
    void showTable(int** arr);
    int main(){
    const int AMOUNT_FLOOR=7;
    const int AMOUNT_PARKING=10;
    int **parking_and_floor[AMOUNT_FLOOR][AMOUNT_PARKING];
    int exit=1;
    do
    {
    show table(**parking_and_floor);
    int floor=0,parking=0;

    for(int i=0; ;i )
    {
    cout<>floor;
    if(floor8)
    {
    cout<<"there is no "<<floor<<" floor!Please choose between 1-7: "<0||floor<8);
    int I = 0;
    for(int i=0;i<10;i ){
    sum+=**parking_and_floor[floor][i];
    };
    if(sum==0){
    cout<<"There is no place in this floor"<<endl;
    show table(**parking_and_floor);
    }else{
    break;
    };
    };
    for(int i=0; ;i )
    {cout<>parking;
    if(parking10){
    cout<<"There is no parking place "<<parking<<". Please choose between 1-10: "<0||parking<11);
    if(parking_and_floor[floor][parking]==0)
    {
    cout<<"This place is reserved, please choose another one";
    show table(**parking_and_floor);
    }else
    { break;
    };
    };
    parking_and_floor[floor][parking]=0;
    cout<<"Your choice: floor "<<floor<<endl<<"place: "<<parking<<" ;"<<endl<<" was reserved succesfully!"<<endl<>exit;
    }while(exit!=0);
    return 0;
    }
    void showTable(int** arr){
    cout<<"Table of parking place"<<endl;
    for(int i=0;i<7;i )
    {
    cout<<"floor"<<i+1;
    for(int j=0;j<10;j )
    {
    arr[i][j]=j+1;
    cout<<arr[i][j]<<"|";
    };
    cout<<endl<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    };
    };

    1. oh look there pieces of code missing
      #include
      using namespace std;
      void showTable(int** arr);
      int main(){
      const int AMOUNT_FLOOR=7;
      const int AMOUNT_PARKING=10;
      int **parking_and_floor[AMOUNT_FLOOR][AMOUNT_PARKING];
      int exit=1;
      do
      {
      show table(**parking_and_floor);
      int floor=0,parking=0;

      for(int i=0; ;i )
      {
      cout<>floor;
      if(floor8)
      {
      cout<<"there is no "<<floor<<" floor!Please choose between 1-7: "<0||floor<8);
      int I = 0;
      for(int i=0;i<10;i ){
      sum+=**parking_and_floor[floor][i];
      };
      if(sum==0){
      cout<<"There is no place in this floor"<<endl;
      show table(**parking_and_floor);
      }else{
      break;
      };
      };
      for(int i=0; ;i )
      {cout<>parking;
      if(parking10){
      cout<<"There is no parking place "<<parking<<". Please choose between 1-10: "<0||parking<11);
      if(parking_and_floor[floor][parking]==0)
      {
      cout<<"This place is reserved, please choose another one";
      show table(**parking_and_floor);
      }else
      { break;
      };
      };
      parking_and_floor[floor][parking]=0;
      cout<<"Your choice: floor "<<floor<<endl<<"place: "<<parking<<" ;"<<endl<<" was reserved succesfully!"<<endl<>exit;
      }while(exit!=0);
      return 0;
      }
      void showTable(int** arr){
      cout<<"Table of parking place"<<endl;
      for(int i=0;i<7;i )
      {
      cout<<"floor"<<i+1;
      for(int j=0;j<10;j )
      {
      arr[i][j]=j+1;
      cout<<arr[i][j]<<"|";
      };
      cout<<endl<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
      };
      };

  6. (floorsAndParkings[floor – 1][parkingPlace – 1] !clarify please, what do the units in square brackets mean,what do they mean

Leave a Reply

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