If Dimensionality of an array is two ,then this types of array is called two dimensional or 2-D array.2-D Array can contain more than one row and more than one column.This is not an linear array but it is the collection of the Linear array. In other word we can say that 2-D array is the set of 1-D array.
2-D Array is the Matrix representation of Mathematics in Computer Programming.
Declaration & Initialization of 2-D Array
In C and C++ ,Syntax of 2-D array is following :
DataType NameOfArray [Size of Dimension-1][Size of Dimension-2]
- Similar to the 1-D array ,First we have to define DataType of array ,Data types can be int , float,char ,bool etc
- Similarly 1-D array , NameOfArray can be any valid name , means It depend upon the programming Language restriction. Like In C & C++ we can not give any name which start with a number.Example : 12ArrName this is wrong name of array in C & C++.
- Size of Dimension define the length of array for Row and Column both., means how many block of memory compiler will allocate for the array, and this allocated Size will be fixed. we can not grow out of the allocated size.Size can be any positive integer.Dimension Size always written inside the [].
We can declare an 2-D array as following :
int Arr2D[10][10] // Arr2D is array of integer of size 10,10.
char Arr2D[8][2] // Arr2D is array of char of size 8,2.
bool Arr2D[20][9] // Arr2D is array of bool of size 20,9.
Similarly For 2-D array Initialization , we can do by two ways :
- First way : we can declare an array and then initialize the data into declared array.
- Second way : we can initialize an array during the declaration.
int Arr2D[2][3] // Array Declaration
Arr2D[2][3]={ {90,100,200},{80,20,104} } //Initialization of Arr2D.
char Arr1D[2][3] = { {'N','C','K'},{'D','S','O'} } // declaration and initialization
- We have to initialize the data into array as per the data type, means if we our Array data type is int and we initialize it by another data type like char,bool etc, So this is wrong implementation of array,means if Array Data type is int then we can initialize integer type data element into an array . Example :
int Arr2D[2][2]={ {'A','B'},{'C','E'} }; // Wrong initialization of an array
Here Arr2D[2][2] is int type ,but we storing char type data,So this is wrong initialization
Accessing of 2-D Array
- We can access the array by the index value of every element.
- We can also access by the address value of every element , for this we need one extra pointer variable to access it.
Example :
/*Index Accessing*/
#include <stdio.h>
#define LSIZE 2 //size of dimension-1(Number of Row)
#define RSIZE 3 //size of dimension-2(Number of Column)
int main()
{
int Arr[LSIZE][RSIZE] ={ {20,90,100},{60,19,200} }; // This is Array Initialization.
printf("%d\n",Arr[0][1]); // Print the value of Arr[0][1]
printf("%d",Arr[1][1]); // Print the value of Arr[1][1]
return 0;
}
/*Pointer Accessing*/
#include <stdio.h>
#define LSIZE 2 //size of dimension-1(Number of Row)
#define RSIZE 3 //size of dimension-2(Number of Column)
int main()
{
int Arr[LSIZE][RSIZE] ={ {20,90,100},{60,19,200} }; // This is Array Initialization.
// Initialization of an pointer p which contain the address of Base Element(Arr[0][0])
int (*p)[3] =Arr;
printf("%d\n",*(*(p+0)+0)); // Print the value of Arr[0][0]
printf("%d\n",*(*(p+0)+1)); // Print the value of Arr[0][1]
printf("%d\n",*(*(p+1)+2)); // Print the value of Arr[1][2]
return 0;
}
Storing Method of 2-D Array
For 2-D or Multi-Dimensional array, we use an specific method to store the elements in a linear storage such as random access memory.
Storing Method define that, how all the data elements will arrange into memory. There are two ways of storing :
1- Row Major Order
2- Column Major Order
- Row Major Order : In this method,elements will store contiguously in memory,Elements will be added row-wise ,means Row-0 to Row-n .
In Memory all elements will store Linearly or in contiguously :
For finding the random element address ,in Row major order we use following formulae to calculate the address of any random element :
Address of A[i][j] = B + w * [ n * ( i – r ) + ( j – c ) ] where
B=base element address
w= size of an element
n=number of column
m=number of row
i=Row index of element whose address is to be found.
j=Column index of element whose address is to be found.
r=Lower limit of row,By default r=0.
c=Lower limit of column ,By default c=0.
- Column Major Order : Similar to the Row Major Order in Column Major Order elements will store contiguously in memory, Elements will be added column-wise ,means Column-0 to Column-n .
In Memory all elements will store Linearly or in contiguously :
For finding the random element address ,in Column major order we use following formulae to calculate the address of any random element :
Address of A [ i][ j] = B + w * [m * (j – c) + ( i – r ) ] , where
B=base element address
w= size of an element
n=number of column
m=number of row
i=Row index of element whose address is to be found.
j=Column index of element whose address is to be found.
r=Lower limit of row,By default r=0.
c=Lower limit of column ,By default c=0.