English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

the first element X (1, 1) is 3000, find the address of X (8, 5).

2006-09-22 05:37:52 · 3 answers · asked by shailesh s 1 in Computers & Internet Programming & Design

3 answers

main ()
{
int a[9][7];
printf ("%d\n", ((int) &a[7][4])-((int)&a[0][0]) + 3000);
}

The answer is
3212

Two-dimensional arrays are stored sequentually, row by row. The size of each row is 7*4=28 bytes. The 7 rows before the one in question take 7*28=196 bytes. The 4 first elements in the 8th row are 16 more bytes, which brings the total 212. Plus the starting address, get 3212.

(note, that I use [7][4], instead of [8][5], because the first element is indexed [0][0] here, not [1][1] - it's the same thing really - just a matter of the notation).

2006-09-22 06:03:49 · answer #1 · answered by n0body 4 · 0 0

this has two answers..based on whether ur language uses row-major or column-major. in row-major, the first element is X(1,1) and the next is X(1,2). that is all elements of a row are together. In column major, the elements of a column are contiguous. so if X(1,1) is the first element, then X(2,1) is the second.
Now, i'll give a function for getting the memory of X(p,q) of a 2d array of size X(m,n), assuming row-major. u can change for column-major, if required.
Address(p,q) = ((p-1)*n + (q-1))*size_of_each_element + base_address

that is, there are n elements in each of the (p-1) rows before the current row, and (q-1) elements in this row already. thus u get total number of elements. multiply this by the size of ezch element, here 4 bytes, to get the displacement of X(p,q) from X(1,1). Add the base address, here 3000 to get the required address.

u'll get the answer 3212 for this.

2006-09-22 07:22:26 · answer #2 · answered by Prancing Stallion 2 · 0 0

depends on how the array is stored in memory? Is it x(0,0), x(1,0), x(2,0), x(3,0) or x(0,0), x(0,1), x(0,2), ....


1 2 3 4 5 6 7 8 9
1 3000 3000 + (9-1)*4
2 3000 + (10-1)*4
3
4
5
6

Addr = 3000 + ((8-1) * 4) + ((5-1) * 9 * 4) = 3172

2006-09-22 05:50:06 · answer #3 · answered by Grant d 4 · 0 0

fedest.com, questions and answers