X
ONLINE BOOKING
BOOK NOW
OFFLINE BOOKING
Call or WhatsApp 7993732682 (WhatsApp Now), 9177341827 (WhatsApp Now)
X
• Home
• Legal Disclaimer
• Terms & Conditions
• Return Policy
• Need any help?? write to us at

support@engineershub.co

X
Go Back
Go Back
Go Back
OR
Fill Up a Simple Form
Fill all the below details correctly and click on Next
Go Back
How can we declare and initialize 2D arrays? Explain with examples.? - EngineersHub
Go Back Question
Naseem Shaik
2 years ago 2 years ago

An array consisting of two subscripts is known as a two-dimensional array. These are often known as an array of the array. In two-dimensional arrays, the array is divided into rows and columns. These are well suited to handle a table of data. In a 2-D array, we can declare an array as :

Declaration:-

Syntax: data_type array_name[row_size][column_size];

Ex:- int arr;

where the first index value shows the number of the rows and the second index value shows the number

of the columns in the array.

Initializing two-dimensional arrays:

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

Ex: int a={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

above statement can also be written as

int a = {{ 0,0,0},{1,1,1}};

by surrounding the elements of each row by braces.

We can also initialize a two-dimensional array in the form of a matrix as shown below

int a={

{0,0,0},

{1,1,1}

};

When the array is completely initialized with all values, explicitly we need not specify the size of the first dimension.

Ex: int a[]={

{0,2,3},

{2,1,2}

};

If the values are missing in an initializer, they are automatically set to zero.

Ex: int a={

{1,1},

{2}

};

Will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero.

### Users Joined mahek thakur
1 month ago sowmya
1 month ago biraja
2 months ago Diana R
2 months ago P Annapoorani
2 months ago
X
How can we declare and initialize 2D arrays? Explain with examples.?
X
How can we declare and initialize 2D arrays? Explain with examples.?

An array consisting of two subscripts is known as a two-dimensional array. These are often known as an array of the array. In two-dimensional arrays, the array is divided into rows and columns. These are well suited to handle a table of data. In a 2-D array, we can declare an array as :

Declaration:-

Syntax: data_type array_name[row_size][column_size];

Ex:- int arr;

where the first index value shows the number of the rows and the second index value shows the number

of the columns in the array.

Initializing two-dimensional arrays:

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

Ex: int a={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

above statement can also be written as

int a = {{ 0,0,0},{1,1,1}};

by surrounding the elements of each row by braces.

We can also initialize a two-dimensional array in the form of a matrix as shown below

int a={

{0,0,0},

{1,1,1}

};

When the array is completely initialized with all values, explicitly we need not specify the size of the first dimension.

Ex: int a[]={

{0,2,3},

{2,1,2}

};

If the values are missing in an initializer, they are automatically set to zero.

Ex: int a={

{1,1},

{2}

};

Will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero.  x