What is Row major and Column major In C Programming ?

Row major and Column major In C

what is row major and column major in array
Row major and Column major in C
In this article we will be going to discuss Row major and Column major in C, in detail after reading this article you will get following queries answer such as, row and column major in 2d array, difference between row  major and column major, what is column major order and row major order, row major order example, what is row major and column major formula etc.

Row major and Column major in C:

As we know that in Two-Dimensional array has two subscript one is stand for row and another is stand for column, by which elements in 2D array are arrange either row wise or column wise, so if we want to find addresses of each elements in 2D arrays than there we use Row major order and Column major order, the address of an individual element in 2D arrays is may different during row major and column major.

For Example:-

If we want to find address of A[1,2] where A is a 2D array and [1,2] indicate that the value present at 1-row and 2-column, so we can find the address of this element(A[1,2]) either by row major order or column major order but the address of element(A[1,2]) would be different from other means that if lets suppose the address of element(A[1,2]) is obtain by row major is 1000 than during column major this address would be different of same element.

Row Major Order:


In this method the element are sorted row-wise, n element of row are sorted in first n location, n element of second row are sorted in next n location and so on. 

Formula For Row-Major Order in C:


This formula is valid only when, we measure the address of  element row-wise...

Base Address + weight of element [ no of column * i) + j ]

If the base address is given in question then well and good but, if the base address is not given then we suppose base address itself randomly,

Weight of element is nothing but, it is datatype size of element which is present in 2D-array.

no of column means total number of column in matrix.

i and j is nothing but, i is stand for particular row position of element and j is stand particular column position of element in matrix.

Example on Row-Major Order:

Ques-1: Lets suppose we want to calculate the address of element A[1,2] and matrix is 2*4.

Solution:  int A[2][4];

It can be calculated as follow:

let base address = 1000

weight of element = 2 (because int datatype size = 2 byte)

row(i) = 1

column(j) = 2

Now put all these value in above formula and calculate the address of the element A[1,2]

= 1000 + 2*[(4*1)+2]

=1000 + 2*[4+2]

=1000 + 2*6

=1000 + 12

=1012 is the address of A[1,2]


Column Major Order:


In this method the element are sorted column-wise, n element of column are sorted in first n location and n element of second column are sorted in next n location and so on. 

Formula For Column-Major Order in C:


This formula is valid only when, we measure the address of  element column-wise...

Base Address + weight of element [ no of row * j) + i ]

If the base address is given in question then well and good but, if the base address is not given then we suppose base address itself randomly,

Weight of element is nothing but, it is datatype size of element which is present in 2D-array.

no of row means total number of row in matrix.

i and j is nothing but, i is stand for particular row position of element and j is stand particular column position of element in matrix.

Example on Column-Major Order:

Ques-1: Lets suppose we want to calculate the address of element A[1,2] and matrix is 2*4 by column major order.

Solution:  int A[2][4];

It can be calculated as follow:

let base address = 1000

weight of element = 2 (because int datatype size = 2 byte)

row(i) = 1

column(j) = 2

Now put all these value in above formula and calculate the address of the element A[1,2]

= 1000 + 2*[(2*2)+1]

=1000 + 2*[4+1]

=1000 + 2*5

=1000 + 10

=1010 is the address of A[1,2] 

So here we can observe that address of same element in matrix is obtain different by row-major order and column-major order respectively.


Conclusion:


In this article we have learned about row-major order and column-major order in detail with example, I hope after reading this article you have no any doubt remaining in this topic but, still if you have any queries then feel free ask in comment section.

Post a Comment (0)
Previous Post Next Post