position: fixed; top: auto !important; margin-left: 112px;

Two dimensional (2D) arrays

As we discussed in our previous article on One Dimensional array, data was organized linearly in only one direction.

int a[5] = {1,2,3,4,5};

Two Dimensional array provides the way to store data in the form of grids and tables. You can think of the two-dimensional array as an array of one-dimensional array.


From the figure, you might notice that there are three one dimensional arrays in three rows.

Syntax:

datatype array_Name[row_size][column_size]

Two Dimensional arrays are specified using two subscripts where the first subscript denotes the row and the second denotes the column.

Suppose, you want to store marks of two students in three subjects then two-dimensional array can be declared like this,

marks[2][3]

Initializing Two-dimensional Arrays

2d arrays are initialized using the same syntax as one dimensional array,

int marks[2][3] = {88,87,76,88,76,56}

As you see, for marks[2][3] dimensional array there are 6 elements . In generalize term, we can say for arr[m][n] dimensional array, there will be m X n number of elements in an array.

You can think about the given initialized array of marks like this also,

int marks[2][3] = {{88,87,76}, {88,76,56}}

Initialize the entire two dimensional array to zeros,

 int marks[2][3] = 0;

The individual elements of two dimensional array can be initialized using the assignment operator,

marks[0][1] = 87;

Simple program to understand the Two dimensional array in action

Program to print the elements of 2d array in C,

#include <stdio.h>
#include <conio.h>
int main()
{
   int arr[2][2] = {10,11,12,13};
   int i,j;
   for(i = 0; i<2; i++)
   {
     printf("\n");
     for(j = 0; j<2; j++)
     {
       printf("%d\t", arr[i][j]);
     }
   }
return 0;
}
}

Output

10   11
12   13

How two dimensional arrays are stored in memory ?

There are two ways of storing the two-dimensional array in memory.

  • Row major order
  • Column major order

Row-major order

The elements in row-major order are stored row by row in memory. Elements of the first row will store before the second and next one. You can also compute the address of an element in row-major order by the following formula,

Address(A[I][J]) = Base_Address + w(N(I - 1) + (J - 1))
where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.

Column-major order

The elements in column-major order are stored column by column in memory. Elements of the first column will store before the second and next one. You can also compute the address of an element in column-major order by the following formula,

Address(A[I][J]) = Base_Address + w(M(J - 1) + (I - 1))

where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.



Solve the Quiz of Article

1) Does this formual compute the address of row-major order?
Address(A[I][J]) = Base_Address + w(M(J - 1) + (I - 1)) 
where, w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.
Yes
No

2) Is this declaration int (*ptr)[10]; signifies ptr is an array of pointers to 10 integers?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.