What is Two Dimensional Array and Representation of Two Dimensional Array in Data Structure Using C and C++
What is Two Dimensional Array in Data Structure Using C and C++
If we want to store the elements in tabular form, The C- language provides the Facility to work with tabular data using Two-Dimensional array. The two dimensional array can hold the data values in the form of table containing Rows and Columns. These are also useful to solve the matrices problem of mathematics, so these are also called “Matrices”. @ upbed.in
How to Declare Two Dimensional Array in Data Structure using C and C++
data type array name[Row size] [Column size];
Here Row size specifies the number of rows and Column size represents the number of columns in the array. The total number of elements in the array are rowsize * columnsize.
By this declaration a two dimensional array of 2 rows and 3 columns is created as follows:
For Example-: int num[2] [3];
Here num is a 2-D array with 2 rows and 3 columns.
How to Initialization of Two Dimensional Array in Data Structure using C and C++
2-D array can be initialized in a way similar to that of 1-D arrays.
For example:
int arr[2][3] = { 2 ,6 ,9 ,8 ,4 ,3 } ;
These values are assigned to the elements row-wise, so the values of elements after this initialization are:
arr[0][0] : 2 arr[0][1] : 6 arr[0][2] : 9
arr[1][0] : 8 arr[1][1] : 4 arr[1][2] : 3
While initializing 2-D array we can group the elements row-wise using inner braces.
For example:
The above example can be initialized as follows:
int arr[2][3] = {
{ 2, 6, 9 },
{ 8, 4, 3 }
};
Here the values in the first inner braces will be the values of Row 0 and values in the second inner braces will be the values of Row 1.
How to Accessing and Displaying of Two Dimensional 2D Arrays in Data Structure using C and C++
The individual elements of this array can be accessed by applying two subscripts, where the first subscript denotes the row number and the second subscript denotes the column number. The starting element of this array is num[0][0] and the last element is num[1][2]. The total number of elements in this array is 2 * 3 = 6.
Column 0 | Column 1 | Column 2 | Row |
num[0] [0] | num[0] [1] | num[0] [2] | Row 0 |
num[1] [0] | num[1] [1] | num[1] [2] | Row 0 |
For processing (input/display) 2-D arrays, we use nested for loops. The outer for loop corresponds to the row and the inner for loop corresponds to the column.
Memory Representation of Two Dimensional 2D Array (Row major and Column major)
Row major representation-: In row- major representation elements are stored row by row i.e. n- elements of the first row are stored in first n locations and elements of the second row are stored in next n locations, and so on.
For a linear array, the computer does not keep track of the address of every element. But keeps the track of base address of the array i.e. address of first element of the array(0th element ).
To find the address of ith element, computer uses the formula-:
Address of ith element = base address + w*( i – LB)
Where w is the width of block of array and LB is the lower bound of the array.
Now a similar situation also holds for any two-dimensional array. That is the computer keeps track of base address of the array and computes the address of ARRAY(i, j ) using the formula-:
Address of ARRAY(i ,j ) = BA + i * ( UBC – LBC + 1 ) * W + W * ( j – LBC )
Where BA = Base address of array i.e. address of first element in the array.
UBC = Upper bound of column.
LBC = Lower bound of column.
W = Width of block of array.
Column major representation-: In column- major representation elements are stored column by column i.e. n- elements of the first column are stored in first n locations and elements of the second column are stored in next n locations, and so on.
To find the address of ARRAY(i, j ), computer uses the formula-:
Address of ARRAY(i ,j ) = BA + j * ( UBR – LBR + 1 ) * W + W * ( j – LBR )
Where BA = Base address of array i.e. address of first element in the array.
UBR = Upper bound of row.
LBR = Lower bound of row.
W = Width of block of array.
Example: Let ar[2][3] = { 6, 5, 8, 7, 2, 9 } is an array.
Row major representation of this array-:
ar[0][0] : 6 ar[0][1] : 5 ar[0][2] : 8
ar[1][0] : 7 ar[1][1] : 2 ar[1][2]: 9
Column major representation of this array-:
ar[0][0] : 6 ar[1][0] : 5
ar[0][1] : 8 ar[1][1] : 7
ar[0][2] : 2 ar[1][2] : 9