Sunteți pe pagina 1din 4

Two-Dimensional Array In C | Programmerdouts

programmerdouts.blogspot.com/2019/06/two-dimensional-array-in-c.html

Multidimensional Array.
Multi-dimensional is declared as same as One-dimensional arrays,
thus required separate square brackets is require. two-
dimensional require two brackets ,three dimensional required
three brackets and so on.

Two-Dimensional Array
You can Assume a Two -dimensional array as a table which
contains rows and columns
Declaration of Two-dimensional array require two brackets
In first bracket ,you have to specified number of rows and In second
Bracket you have to specified number of columns.
As one-dimensional array, Element of two-dimensional array has
a unique index number.

Declaration of 2-dimensional array


Syntax:

data_type array_name[no of rows ][no of columns];

Let's declare some multi-dimensional arrays.

1/4
#include<stdio.h>

void main()
{
int arr[3][4]; //integer type of Two-dimensional array having 3 rows and 4
columns.

float arr1[3][3]; //float type of Two-dimensional array having 3 rows and 3


columns.
}

Initialization of two dimensional array


For initializing two dimensional array ,you can assume each row as
an individual array.
Two dimensional array are nested arrays.
In one array,there are lots of other arrays.

lets Initialize some two-dimensional array.

#include<stdio.h>

void main()
{ //integer type of Two-dimensional array having 3 rows and 4 columns.
int arr[3][4] = {{98,65,5,87}, // first row.
{32,42,65,64}, //second row.
{31,74,47,14}}; //third row.

//initializing the two dimensional array 'arr'.


}

Element and their indexes.

2/4
Accessing the elements of 2d array.
As we know each element of an array has a unique index number,
with the help of that we will access the elements.

let access some element of array.

#include<stdio.h>

void main()
{ //integer type of multi-dimensional array having 3 rows and 4 columns.
int row1_col1,row2_col2,row0_col0;
int arr[3][4] = {{98,65,5,87}, //First row
{32,42,65,64}, //second row
{31,74,47,14}}; //third row

//initializing the two dimensional array 'arr'

row0_col0 = arr[0][0];
row1_col1 = arr[1][1];
row2_col2 = arr[2][2];

//printing element at index arr[0][0]


printf("\nElement at 0_row and 0_col is %d",row0_col0);
//printing element at index arr[1][1]
printf("\nElement at 1st_row and 1st_col is %d",row1_col1);

//printing element at index arr[2][2]


printf("\nElement at 2nd_row and 2nd_col is %d",row2_col2);
}

3/4
Output:
Element at 0th_row and 0th_col is 98
Element at 1st_row and 1st_col is 42
Element at 2nd_row and 2nd_col is 74

Further Concepts:
How to initialize Two Dimensional Array?
How to take Elements of Two dimensional array from the User?
How to display 2D Array As an Table?
Practice Program Regarding Arrays

Practice Programs
Programs regarding Arrays
Programs Regarding 2d arrays
Programs regarding matrix
And lot more

Further Topics:

People Also Searched:

4/4

S-ar putea să vă placă și