answersLogoWhite

0


Best Answer

The memory of a computer is linear and not a matrix like a 2D array. So, the elements of the array are stored either by row, called "row-major", or by column, called "column-major". Row-major order is used most notably in C and C++ during static declaration of arrays.

In C, since the length of each row is always known, the memory can be filled row one row at a time, one after the other. Example:

a[i][j] =

1 2 3 4 5 6 7 8 9

Representation in the memory: In row-major: 1 2 3 4 5 6 7 8 9 In column-major: 1 4 7 2 5 8 3 6 9

Address calculation of an element: Row-Major : addr (i,j) = B + W * ( Nc * (i - Lr) + (j-Lc) ) Col-Major : addr (i,j) = B + W * ( (i - Lr) + Nr * (j-Lc) ) i,j = subscript number. B = Base address W = width (size) of each element Nc = Number of Columns Nr = Number of Rows Lc = Lower-bound of Column Lr = Lower-bound of Row

In above example, for element (6), i.e., a(1,2) in row-major or a(2,1) in col-major, B = 200 (say) W = 2 Lr=Lc=0 Nc=Nr=3

addr (1,2) = 210; addr (2,1) = 210

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

TREE

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How are two dimensional arrays represented in memory. Explain how address of an element is calculated in a two dimensional array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is the base address of a two dimensional array the address of the first component true or false?

yes


Write A programme of calculating address of one element of multi dimensional matrix?

If the element is a[i][j][k][l], then it's address is &a[i][j][k][l]


How are three dimensional arrays represented in memory Explain how address of an element is calculated in three dimensional array?

Assuming the array is mapped in contiguous memory, the memory block is divided into equal blocks according to the first dimension, and each of those blocks is divided equally according to the second dimension. The smallest blocks therefore represent a one-dimensional array according to the final dimension. To look at it another way, a three-dimensional array is a one-dimensional array where every element is a two-dimensional array. And every two-dimensional array is a one-dimensional array where every element is also a one-dimensional array. This concept can also be extended to four-dimensional arrays and beyond (a four-dimensional array being a one-dimensional array where every element is a three-dimensional array). The address of any element in a contiguous multi-dimensional array is calculated from the type of element in the array and the element index. If we assume the following three-dimensional array has been declared: int a[3][4][5]; If we also assume that an int (integer) is a 32-bit value and a byte has 8 bits, then it can be seen there are 3*4*5*32 bits allocated to the array, which is 1920 bits in total or 240 bytes. We can also say that there are 3 arrays of 4*5 integers or, more simply, 3*4 arrays of 5 integers. An array of 5 integers therefore consumes 5*32 bits, which is 160 bits or 20 bytes. The second dimension tells us there are 4 such arrays, which makes 80 bytes, and the third dimension tells us there are 3 of those, which brings us to 240 bytes. Thus to locate any element, we multiply the last index by 4 (the size of an integer), the middle index by 20 (the size of 5 integers) and the first index by 80 (the size of 20 integers), and add these three results together to obtain the offset from the start of the array. Thus element a[2][3][4] will be found (2*80)+(3*20)+(4*4) bytes from the start of the array, which is 160+60+16 or 236 bytes. This is the address of the last integer in the array, which is fortunate because a[2][3][4] is also the index of the last integer in the array (remember that array indices are zero-based). The name of the array is also a reference to the start address of the array, thus the start address is a, which is the same as saying the address of element a[0][0][0], which is at offset (0*80)+(0*20)+(0*4), which is obviously 0 bytes from a. As well as accessing elements by their indices, we can also point at individual elements using pointer arithmetic. Element a[1][2][3] can therefore be found at memory address a+(1*80)+(2*20)+(3*4), which is a+132 bytes. In point of fact, the index notation is simply syntactic sugar for the pointer arithmetic that is actually done behind the scenes. So much for arrays allocated in contiguous memory. Although there will be few occasions where 240 bytes cannot be allocated easily in contiguous memory, imagine a larger array of integers, say [128][256][512], which is 226 bytes or 64MB. On a 32-bit system, 64MB of contiguous RAM may not be available, so we must split the array into smaller arrays. This is where imagining multi-dimensional arrays as being one-dimensional arrays of one-dimensional arrays comes in handy. If we treat the first two dimensions as a two-dimensional array of integer pointers (each of which is 32-bits), then we only need 128*256*4 bytes, which is only 131,072 bytes or 128kB. Each pointer will reference a separate one-dimensional array of 512 integers, each of which is only 2kB in size (512*4). Although total memory consumption is now 256MB, because the allocation is split into one 128kB allocation and 512 separate 2kB allocations there's a far greater chance the allocation will succeed than if we attempt to allocate 64MB contiguously. The index notation hasn't actually changed but the underlying pointer arithmetic has. If we assume the pointer array is p[128][256], then referencing p[64][128][256] will return the 257th element of the array pointed to by p[64][128]. This works because the first two dimensions return a pointer to a one-dimensional array of integers. If we suppose that the pointer is x, then the final dimension returns the integer stored at x[256]. The underlying arithmetic is a little cumbersome, but can be broken down as x = (p+(64*(256*4))+(128*4)) + (256*4), which reduces to x = (p+66048) + 1024. Thus the memory address stored at offset 66048 bytes within the pointer array plus the offset 1024 returns the 257th element of the appropriate one-dimensional array of integers.


What are the benefits of multidimensional arrays?

Multi-dimensional arrays are accessed using more than one index: one for each dimension. Multidimensional indexing can be reduced internally to linear indexing; for example, a two-dimensional array with 6 rows and 5 columns is typically represented by a one-dimensional array of 30 elements.


What is meant by single array?

A single dimensional array, or one-dimensional array, is a contiguous block of memory that contains one or more elements, each of which can store a single fixed-width value. Since each element is the same length (in bytes), the total memory consumed by the array is equal to the product of the element length and the total number of elements.One-dimensional arrays can be thought of as being a single row or column of numbered pigeon holes (the elements), such that each hole may contain a single object (a value). The numbers identifying each element are a zero-based incremental subscript, or index number. Thus for an array of n elements, the indexes will be in the range 0 to n-1. Some languages permit the first index to be offset from 0 such that the indexes will be in the range offset to offset+n-1, however this does not alter the actual index which is always zero-based.Arrays permit random, constant-time access to any element in the array via the element's index. When accessing an element, the return value is a reference to the first memory address of that element. This is achieved through simple pointer arithmetic, such that element i will be found at memory address (i-1) x (size of element) bytes from the start address of the array (hence the index is always zero-based, regardless of any offset that is applied).The value's stored in the elements must be of the same intrinsic type, but can be variables or constants of any kind, including object references and pointer variables, even pointers to other arrays.Multi-dimensional arrays are merely an extension of one-dimensional arrays, such that each element contains a reference to another one-dimensional array (each element of which may refer to yet another one-dimensional array, and so on). For instance, a two-dimensional array can be thought of as being pigeon holes arranged in rows and columns, such that every row has the same number of columns, and every column has the same number of rows. A three-dimensional array can be thought of as being a cuboid of pigeon holes. Although it's difficult to imagine a four-dimensional array in a three-dimensional space, the easiest way to think of it is as being a one-dimensional array where each element contains a three-dimensional array. More simply, a series of cuboids arranged in a row or a column.Multi-dimensional arrays needn't reside in contiguous memory, however each individual one-dimensional array that makes up a multi-dimensional array must itself reside in contiguous memory.Accessing the individual elements of a multi-dimensional array requires one subscript per dimension. Thus for a cuboid that has 3 rows, 3 columns and 3 tiers, the middle box will be found at zero-based index (1, 1, 1). Again, simple pointer arithmetic is employed to determine the address of the element offset from the starting address of each one-dimensional array.

Related questions

How are three-dimensional arrays represented in memory Explain how address of an element is calculated in three dimensional array?

3-D arrays can be represented as a single dimension of tables. Each table has rows and columns. Each table may also refered as Page. Let a[x][y][z] is an element of a three dimensional array 'a' at the xth Page, Within that page yth row and zth column. In memory it will be stored as sequence of memory locations. Suppose array index starts from 0,0,0. If the first element of the array is stored in location M, The address of the a[i][j][k] = (i-1)U2U3 + (j-1)U3 + (k-1), Where U2 and U3 are the dimention of a table.


A NIC's physical address is a 48-bit address represented in what format?

It's represented in HEX format.


Discuss and explain the address decoding technique?

explain address decoding technique


Is the base address of a two-dimensional array the address of the first component true or false?

True.


Is the base address of a two dimensional array the address of the first component true or false?

yes


Ignou bca 4 semester cs-64 assignment answers?

What is the need of segments in 8086 micro-processor? Explain how the address of an instruction is calculated in 8086 using segment register


What country is represented by pt at the end of an email address?

Portugal


What can you use to identify a ipv6 address?

IPv6 address has 2^128 address and IPv6 address is of 16 bytes and is represented in colon hex notation.


What country is represented by cu at the end of an email address?

herb@laquercia.cu


How function pointer is represented?

A pointer's type does not affect how an address is represented, it affects how the object at that address is to be interpreted. As such, function pointers are represented no differently to any other type of pointer. If the system uses 32-bit addressing than a pointer is 32-bit variable regardless of the type it refers to.


Explain this type of address 169.254.10.67?

Ftqwsftyxsyftg


Both computer instructions and memory address are represented by?

Binary Codes