Pages

Friday, October 6, 2017

Easy tutorial for C / C ++ - Arrays 4 (Two dimensional array)

Easy tutorial for C / C ++ - Arrays 4 (Two dimensional array)

Two dimensional array :

- A two-dimensional array can process more data regularly in the form of a matrix. The basic two-dimensional array declaration method is as follows.

'Data type' 'array name' ['number of rows'] ['number of columns']

'Data type': The data type of the data to be stored in the array.
'Array name': The name of the array to be used.
'Number of rows': Number of rows in a two-dimensional array.
'Number of Columns': Number of columns in a two-dimensional array.

- Let's look at a simple example.

- Define int a[3][4]. It can be thought of as a matrix of 3 rows and 4 columns and can store 12 integer data in total. The order of each element data in the matrix is shown in the following figure.


- Since both rows and columns start at 0, the row index is 0 to 2, and the column index is 0 to 3. You can use the matrix indices of each element to assign or return data to each element.

- Let's look at an example.

Example Code

#include <iostream>
using namespace std;

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

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

          for(int i = 0; i < 3; i++){
                   for(int j = 0; j < 4; j++){
              a[i][j] = v ;
                   v++ ;
                   }
          }

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

- Declared an integer two-dimensional array 'a'. I declared an integer type variable 'v' and assigned 0 as an initial value. You want to assign a value to a two-dimensional array using this 'v' value.

- The first for loops are statements that assign values to a two-dimensional array. 'i' specifies the row index and 'j' specifies the column index, and the for loop is implemented. The assignment order is as follows.

a[0][0], a[0][1], a[0][2], ... , a[2][3]

- At the beginning a[0][0] is assigned the initial value 0 of v, it is incremented by 1 (v ++), and finally 11 is substituted.

- The second for loop is a syntax that prints out data stored in a two-dimensional array in the same order.

results :

2-dimensional array
  a[0][0] = 0
  a[0][1] = 1
  a[0][2] = 2
  a[0][3] = 3
  a[1][0] = 4
  a[1][1] = 5
  a[1][2] = 6
  a[1][3] = 7
  a[2][0] = 8
  a[2][1] = 9
  a[2][2] = 10
  a[2][3] = 11

- You can see the output in the expected order.

- If you change the output part as follows, you can output it as a matrix form. You should add "#include <iomanip>" to the beginning of the program.

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;
          }

results :

2-dimensional array
   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            1            2            3
            4            5            6            7
            8            9          10          11

No comments:

Post a Comment