i2tutorials

Two Dimensional Arrays

Two Dimensional Arrays

 

A list of things is often given one variable name using two subscripts and such a variable is named a two – subscripted variable or a two – dimensional array. 2D arrays are generally known as matrices.

 

Two Dimensional Arrays

 

The above table contains a complete of 15 values.

 

 

In mathematics, we represent a specific value during a matrix by using two subscripts like Vij. Here V denotes the entire matrix Vij refers to the value in the “ i ”th row and “ j ”th column.

For example, within the above table, V23 refers to the worth “80”. C allows us to define such tables of things by using two-dimensional arrays.

They can be declared as:

 

The above table are often defined in C as

 

 int V[5][3];

 

Initialization of 2D Array

 

There are some ways to initialize two Dimensional arrays.

 

int disp[2][4] = { {10, 20, 12, 43}, {14, 25, 16, 67}};
or
int disp[2][4] = { 10, 20, 12, 43, 14, 25, 16, 67};

 

Things to keep in mind when initializing the 2D table –

 

When we give values during one-dimensional array declaration, we don’t need to mention dimension. But that’s not the case with a 2D array; you must specify the second dimension even if you are giving values during the declaration. Let’s understand this with the assistance of few examples .

 

 

Storing Data into 2D array

 

.....
int abc[5][4];
.....
for(i=0; i<=4; i++) //loop for first dimension which is 5 here 
for(j=0;j<=3;j++) //loop for 2D of array which is 4 in this example
{
printf("Enter value for abc[%d][%d]:", i, j); 
scanf(“%d”, &abc[i][j]);
}

 

 

 

Exit mobile version