Multi-Dimensional Arrays
Multidimensional arrays are arrays of arrays are declared.
Int int_array [10] ; // A normal one dimensional array
Int int_array2d[10]; //A two dimensional array
Two dimensional arrays are as a grid. We have ten row and ten columns, for a total of 100 elements. The first number in brackets is the number of rows and ten columns for a total of 100 elements. The first number in brackets is the number of rows ; the second number in brackets is the number of columns. So, the upper left corner of any grid would be element [0] [0]. The element to is right would be [0][1] and so no. here is a little illustration to help.
[0][0]
|
[0][1]
|
[0][2]
|
[1][0]
|
[1][1]
|
[1][2]
|
[2][0]
|
[2][1]
|
[2][2]
|
INTIALIAING A TWO DIMENSIONAL ARRAYS
A 2D array can be initialized like this:
Int array[3][3]= {1,2,3,4,5,6,7,8,9} ;
Int array [3][3]={1,2,3,4,5,6,7,8,9} ;
The results are the same; it's just not as obvious. Also like a normal array, you don't have to specify the numbers in the brackets when you initialize an array, the compiler will figure it out for you.
ACCESSING TWO-DIMENSIONAL ARRAY ELEMENTS
Elements of two dimensional array s can be accessed as follows;
A[0][0] = 10; // assigns 10 to element at first row and first column
A[0][1] = 3 ; // assigns 3 to element at first row and second column
A[2][2] = 45; // assigns 45 to the element at second row and second column
IMPLEMENTATION OF TWO DIMENSIONAL ARRAY
A two-dimensional array can be implemented in a programming language in two ways;
1. Row-major implementation 2. Column-major implementation
Row -major implementation
Row major implementation is a linearization technique in which elements of array are reader from the keyboard row wise i.e.. the complete first row is stored. Then complete second row is stored and so on. For example, an array a [3][3] is stored in the memory as shown in fig. 3.4 below :
a00
|
a01
|
a02
|
a10
|
a11
|
a12
|
a20
|
a21
|
a22
|
Row 1 Row 2 Row 3
The storage can be clearly understood by arranging array as matrix as shown below:"
A = a00 a01 a02 Row 1
a10 a11 a12 Row 2
a20 a21 a 22 Row 3
Above figure shows the actual physical storage of elements of the array, whereas the matrix form is logical representation of array (and is not the actual way in which two-dimensional array is stored).
Address of elements is Row major implementation
The computer does not keep the track of all elements of the array, rather, it keeps a base address (i.e., the address of first element in the array), and calculates the address of required element when needed. It calculates this (in row major implementation) by the following relation:
Address of element a[i][i] = B + W (n i-L1) + ( j-L2))
Where B is the base address of the array, W is size of each array element, n is the number of columns (i.e, U2- L2). L1 the lower bound of row; L2 is lower bound of column. Let us study an example to get clear idea of row major implementation:
Column Major Implementation
In column major implementation memory allocation is done column by column i.e., fist the elements of the complete first column is stored, then elements of complete second column is stored and so on. For example an array a [3][3] is stored in the memory as shown in the Fig. 3.5 below:
a 00
|
a10
|
a20
|
a01
|
a11
|
a 21
|
a 02
|
a 12
|
a 22
|
Column 1 Column 2 Column 3
Fig. 3.5.
Address of element in column major implementation
In Column major implementation address of an element a [i][j] is given by the relation:
Address of element a [I ][j ] = B + W (m (j-L2) + (i- L1))
Where m is the number of rows (i.e., U1- L1).
Data Structure & Algorithms Assignment Help, Live Experts
Struggling with data structure problems? Data structure subject is quite tough to learn? Need quick assistance in data structure questions? ExpertsMind.com is right place for you where your search ends, We at ExpertsMind offer online data structure assignment help, data structure homework help and data structure and algorithms question's answers by best online support by qualified tutors.
ExpertsMind.com - Multi Dimensional Arrays Assignment Help, Multi Dimensional Arrays Homework Help, Multi Dimensional Arrays Assignment Tutors, Multi Dimensional Arrays Solutions, Multi Dimensional Arrays Answers, Arrays Assignment Tutors