Pages

Saturday, October 7, 2017

Easy tutorial for C / C ++ - Arrays 5 (Two dimensional array - initial and address values)

Easy tutorial for C / C ++ - Arrays 5 (Two dimensional array - initial and address values)

The initial value of a two-dimensional array :

- Let's look at how to give initial values to a two-dimensional array. Similar to a one-dimensional array, you can give initial values the same as the number of rows and columns or less. 

- Let's look at an example.

Example Code

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
     int a[3][4] = {0} ;

     int b[3][4] = {  {0,1,2,3},
                               {4,5,6,7},
                               {8,9,10,11}  };

     int c[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

     cout << "2-dimensional arrays " << endl;

     for(int i = 0; i < 3; i++){
                   for(int j = 0; j < 4; j++){
              cout << setw(5) << "a[" << i << "][" << j << "]" ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 3; i++){
                   for(int j = 0; j < 4; j++){
              cout << setw(10) << a[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 3; i++){
                   for(int j = 0; j < 4; j++){
              cout << setw(10) << b[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 3; i++){
                   for(int j = 0; j < 4; j++){
              cout << setw(10) << c[i][j] ;
                   }
                   cout << endl;
          }
          return 0;
}

- For a two-dimensional array 'a', we gave only one initial value. In the case of a two-dimensional array 'b', a total of 12 values were given as initial values using braces indicating three lines in the whole brace. In the case of 'c', we gave a total of 12 values in a row.

- Let's compare the values stored in the two-dimensional array 'a, b, c'.

results :

2-dimensional arrays
   a[0][0]   a[0][1]   a[0][2]   a[0][3]
   a[1][0]   a[1][1]   a[1][2]   a[1][3]
   a[2][0]   a[2][1]   a[2][2]   a[2][3]

            0            0            0            0
            0            0            0            0
            0            0            0            0

            0            1            2            3
            4            5            6            7
            8            9           10           11

            0            1            2            3
            4            5            6            7
            8            9           10         11

- In the case of a two-dimensional array 'a', we gave only one initial value, and all the remaining values are initialized to zero.

- Two-dimensional arrays 'b' and 'c' have the same result. This means that 'b' and 'c' are the same initialization method.

The address value of a two-dimensional array :

- Let's see how the address values are stored in a two-dimensional array. Let's look at an example to print the address of a two-dimensional array ('a [2] [3]').

Example Code

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
     int arr[2][3] = {  {0,1,2},
                               {3,4,5}  };

     cout << "2-dimensional array " << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(10) << "arr[" << i << "][" << j << "]" ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << arr[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << &arr[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;
      return 0;
}

results :

2-dimensional array
              arr[0][0]             arr[0][1]             arr[0][2]
              arr[1][0]             arr[1][1]             arr[1][2]

                         0                         1                         2
                         3                         4                         5

 0x7fff8174d0e0  0x7fff8174d0e4  0x7fff8174d0e8
 0x7fff8174d0ec  0x7fff8174d0f0  0x7fff8174d0f4

- It can be seen that it increases by 4 bytes as integer type data. And the order increases in order of data values 0, 1, 2, 3, 4, 5.

No comments:

Post a Comment