Page 52 - DCAP407_DATA_STRUCTURE
P. 52
Unit 3: Arrays
{
for(k=0;k<3;k++)
{
printf("%d\t",table[i][j][k]);
}
printf("\n");
}
}
printf(“%d”, table[1][1][1]);
For every iteration of the i, j and k loops, the values printed are:
[0][0][0] = 1
[0][0][1] =2
[0][0][2] =3
[1][1][1] =14
Representing Two-Dimensional Arrays in Memory
Let M be a two-dimensional a x b array. Although, M is pictured as a rectangular array of elements
with a rows and b columns, the array will be represented in the memory as a block of a x b sequential
memory locations. Specifically, the programming language will store the array M either column wise or
row wise. When the programming language stores an array column wise, it is known as column-major
order and when it stores row wise it is known as row-major order.
The figure 3.2 depicts the two storage ways when M is a two-dimensional 3 x 4 array.
Figure 3.2: Two-dimensional 3 x 4 Array
Source: Lipschutz, S. Data Structures with C. Delhi: Tata McGraw-Hill. Page 4.31.
Now, consider the two-dimensional a x b array M. The computer keeps track of Base (A) – the address
of the first element M[1,1] of M and computes the address LOC (M[J,K]) of M[J,K] using the formula:
For column-major order,
LOC (M[J,K]) = Base(M) = w[P(K-1) +(J-1) --- (1)
LOVELY PROFESSIONAL UNIVERSITY 45