edited by
2,348 views
4 votes
4 votes
what is simple formula of address calculation in

1)row major order

2)column major order        i think it should be simple enough as we have to just see the differerence  from base address

 

and what about  if some negative subscripts are given like a[12,-5} is it possible for an array to have negative subscript
edited by

1 Answer

Best answer
4 votes
4 votes

Let us consider an integer array of size $m*n$ and size-of-int be $\tt s$ Bytes.

Case1: If array indices start with $0$ (default in C)

In Row Major Order to access an element a[i][j], we are supposed to cross $i$ rows each with elements equal to number of columns and $j$ columns. So, $\color{navy}{\&a[i][j] = (i*n+j)*s + base}$ And similarly $\color{navy}{\&a[i][j] = (j*m+i)*s + base}$ when array is stored in CMO.

Case2: If array indices start with $1$

RMO :$\color{navy}{\&a[i][j] = ((i-1)*n+j-1)*s + base}$

CMO: $\color{navy}{\&a[i][j] = ((j-1)*m+i-1)*s + base}$

Yes, negative subscripts are allowed in C. a[i][j] = *(*(a+i)+j). Here address is calculated using pointer airthematic. Same is the reason why a[11] gives no error for an array of 10 elements.

edited by

Related questions

0 votes
0 votes
1 answer
1
viral8702 asked Apr 29, 2022
447 views
A frame buffer array is addressed in row major order for a monitor with pixel locations starting from (0,0) and ending with (100,100). What is address of the pixel(6,10)?...
1 votes
1 votes
2 answers
2
6 votes
6 votes
0 answers
3
V pandey asked Oct 8, 2022
3,017 views
Consider a multi-dimensional array A [90][30]40] with base address start at 1000, calculate the address of A[10][20][3] in row major order and column major order. Assume ...
5 votes
5 votes
2 answers
4
srestha asked Jan 16, 2017
5,487 views
Assume that an upper triangular matrix A[0... 99, 0 ...99] is stored in a linear array C of size 5050 with row major order. If A[0, 0] is stored in C[0], find the index o...