/* */ Click to Join Live Class with Shankar sir Call 9798158723

Two Dimensional Array in C++.


Arrays with two sets of square brackets [][] are called two-dimensional arrays. A two dimensional array is used when data items are arranged in row-wise and column-wise in a tabular form. The array has two sets of square brackets [3][4], and hence it is a 2 dimensional array with 3 rows and 4 columns. This declaration informs the compiler to reserve 12 locations(3*4=12)contiguously one after the other.


Initialization of two dimensional array.


Assigning required value to a variable before processing is initialization. As we initialize a variable to the required value we can initialize the individual elements of a 2 dimensional array during initialization.

Syntex :-

data type array_name[rows][cols]={
{a1,a2,a3...an},
{b1, b2, b3...bn},
};

Example :-
int arr[2][3]={
{33, 55, 66},
{23, 33, 12},
};

Q.) This program initializes 8 elements in a two-dimensional array of size four rows and two columns, then prints the array on output:
    #include< iostream>
using namespace std;
int main()
{
    int arr[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    int i, j;
    cout<<"The Two-dimensional Array is:\n";
    for(i=0;  i< 4; i++)
    {
        for(j=0; j< 2; j++)
            cout<< arr[i][j]<<"  ";
        cout<< endl;
    }
    cout<< endl;
    return 0;
}
Output
The Two-dimensional Array is
1   2
3   4
5   6
7   8