C++/C++

Advertisement


Question
Thanks for ur answer. But that was not I meant.
THere are two ways to implement a 2d array. Row major implementation and column major implementation.
In case of row major implementation, the address of an element is calculated by the formula.


address of A[i, j] = address of A + (((i - row_L) * n)
         + ( j - col_L)) * element_size
         

where address of A is the address of the first element in the array (address of A[row_L,col_L])
row_L is the lower bound for the row index (starting index)
col_L is the lower bound for the column index (staring index)
n is the number of elements in a row.

 Similarly, if the array is implemented in column major order, what is the formula to compute the address of an element.  

Answer
tsv Menon, To obtain the formula for addressing an array in column-major order, simply interchange the rows and columns in the formula for addressing an array in row-major order.

Assume (for simplicity): Array indices start at 0; first index is row, second is column; there are r rows and c columns.

Row-major order (column changes most rapidly):

address of A[i, j] = address(A(0,0)) + ElementSize * (i * c + j)

Column-major order (row changes most rapidly):

address of A[i, j] = address(A(0,0)) + ElementSize * (j * r + i)

Example:

In a 'char' array of size (2, 3) in row-major order, A(1, 0) is located at offset 3:

0: A(0,0)
1: A(0,1)
2: A(0,2)
3: A(1,0) <--
4: A(1,1)
5: A(1,2)

In a 'char' array of size (2, 3) in column-major order, A(1, 0) is located at offset 1:

0: A(0,0)
1: A(1,0) <--
2: A(0,1)
3: A(1,1)
4: A(0,2)
5: A(1,2)

David

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.