A list of items can be given one variable name using two subscripts and such a variable is called a two – subscripted variable or a 2d array in c++.
The syntax of TWO – DIMENSIONAL ARRAY is :
data_type array_name[row size] [column size] ;
The Example of TWO – DIMENSIONAL ARRAY is :
int V[5][3];
Also Read This :- Array in c++
Initializing 2d array in c++ :
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.
int table[2] [3] = {0,0,0,1,1,1};
This initializes the elements of the first row to zero and the second row to one.
This initialization is done row by row.
The above statement can be equivalently written as :
int table[2][3] = {0,0,0},{1,1,1};
we can also initialize a two – dimensional array in the form of a matrix as shown.
int table[2][3] = { {0,0,0}, {1,1,1} };
Commas are required after each brace that closes of a row, except in the case of the last row.
#include <iostream> #include <conio.h> using namespace std; main() { Â Â int num[2][5] = { Â Â Â Â Â Â {3, 5, 6, 7, 3}, // 0 Â Â Â Â Â Â {1, 2, 4, 9, 8} Â // 1 Â //index 0 Â 1 Â 2 Â 3 Â 4 Â Â Â Â Â Â Â Â Â Â }; Â Â cout << "Element of [0][3] index is " << num[1][3]; Â Â getch(); Â Â return (0); }
output
Element of [0][3] index is 9