# Data Structure 2D Array

## 2D Array

An array of arrays or a 2D array is organized as matrices. It can be represented as a collection of rows and columns and is used to implement a relational database look-alike data structure. For holding the bulk of data at once, a 2D array provides an easy way. This data can then be passed to any number of functions wherever required.

### Declaring 2D Array

Declaring a two-dimensional array and a one-dimensional array is very much similar. In a two-dimensional array, the elements are organized in the form of rows and columns, where the first element of the first row is represented by a. Here, the number of the first index represents the number of the row and the number of the second index represents the number of the column. Syntax:

int arr[max_rows][max_columns];


### Accessing data in a 2D array

Similar to one-dimensional arrays, the elements of 2D arrays can be randomly accessed and the individual cells can be accessed by using the indices of the cells. In a 2D array, a particular cell has two indices attached to it. The first index is its row number while the other is its column number.

Syntax: To store the value stored in any particular cell of a 2D array to some variable x:

int x = a[i][j];


where

• i is the row number of the cell.
• j is the column number of the cell.

Assign each cell of a 2D array to 0 Example:

#include   int main() { int a, i, j, n = 5; for (i=0; i&lt;n; i++) { for (j=0; j&lt;n; j++) { a[i][j] = 0; printf("%d\t",a[i][j]); } printf("\n"); } }

Output Initializing 2D Arrays:
There is no need to specify the size of the array while declaring and initializing a one-dimensional array in C programming simultaneously. However, with 2D arrays, we need to define at least the second dimension of the array.

Syntax: To declare and initialize the 2D array:

int a = {1,2,3,4};


Number of elements present in a 2D array

(number of rows * number of columns)


## Storing the data into a 2D array Example:

### C Example:

#include void main () { int a,i,j; for (i=0;i&lt;2;i++) { for (j=0;j&lt;2;j++) { printf("Enter a[%d][%d]: ",i,j); scanf("%d",&amp;a[i][j]); } } printf("\nThe array is:: \n"); for(i=0;i&lt;2;i++) { for (j=0;j&lt;2;j++) { printf("%d\t",a[i][j]); } printf("\n"); } }

Output ### Java Example:

import java.util.Scanner; public class Main { public static void main(String[] args) { int[][] a = new int; Scanner sc = new Scanner(System.in); for (int i=0;i&lt;2;i++) { for(int j=0;j&lt;2;j++) { System.out.print("Enter Element: "); a[i][j]=sc.nextInt(); } } System.out.println(); System.out.println("The array is::"); for(int i=0;i&lt;2;i++) { for(int j=0;j&lt;2;j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } } }

Output ### C# Example:

using System;   public class Program { public static void Main() { int[,] a = new int[2,2]; for (int i=0;i&lt;2;i++) { for (int j=0;j&lt;2;j++) { Console.WriteLine("Enter Element: "); a[i,j]= Convert.ToInt32(Console.ReadLine()); } } Console.WriteLine(); Console.WriteLine("The array is::"); for (int i=0;i&lt;2;i++) { for (int j=0;j&lt;2;j++) { Console.Write(a[i,j]+" "); } Console.WriteLine(); } } }

Output ### Mapping 2D array to 1D array:

A 2D array exists from the user point of view, i.e., they are created to implement a relational database table look-alike data structure. However, in computer memory, the storage technique for a 2D array is similar to that of a one-dimensional array and the size of a 2D array is equal to the multiplication of the number of rows and the number of columns present in the array. Thus, to store them in memory, we need to map a two-dimensional array to a one-dimensional array.

### Techniques for storing 2D array elements into memory:

Row Major ordering:

All the rows of the 2D array, in row-major ordering, are stored into the memory contiguously, i.e., the 1st row of the array is first stored into the memory completely, after which the 2nd row of the array is stored into the memory completely and this process continues till the last row.

Column Major ordering:
All the columns of the 2D array, in column-major ordering, are stored into the memory contiguously, i.e., the 1st column of the array is first stored into the memory completely, after which the 2nd row of the array is stored into the memory completely and this process continues till the last column.

## Calculating the address of the random element of a 2D array:

We have two different formulas to calculate the address of a random element of the 2D array, because of the existence of two different techniques of storing the two-dimensional array into the memory.

### By Row Major Order:

Considering an array a[m][n], where m is the number of rows while n is the number of columns. Now, if BA is the base address or the address of the first element of the array a, then the address of an element a[i][j] of the array stored in row-major order is:

Address(a[i][j]) = BA + (i * n + j) * size


Example:
Consider an array a[10…30, 55…75], where, base address of the array (BA) = 1000,  and size of an element = 8 bytes. Find the location of a.

Address(a) = 1000 +
((15 - 10) x (68 - 55 + 1) + (68 - 55)) x 8
= 1000 + (5 x 14 + 13) x 8
= 1000 + 83 x 8
= 1664


### By Column major order:

Considering an array a[m][n], where m is the number of rows while n is the number of columns. Now, if BA is the base address or the address of the first element of the array a, then the address of an element a[i][j] of the array stored in column-major order is:

Address(a[i][j]) = ((j*m)+i)*size + BA