How to Print Out Contents of an Array in Row Major Fashion C
Become FREE domain for 1st year and build your brand new site
In this commodity, nosotros take explained the idea of Row major and Column major guild which is used to store multi-dimensional array equally a one-dimensional array.
Table of contents:
- Introduction to Array
- Row Major Order
- Column Major Order
- Finding address of chemical element given the alphabetize
- Comparing of Row Major Order VS Column Major Order
- How to determine if elements are stored in row major or column major order?
Prerequisite: Basics of Array
Let us go started with Row major and Column major club.
Introduction to Assortment
Nosotros know that elements of a linear array are stored at contiguous memory locations. This means that for an array a = [1,2,3,iv], if the first element is stored at memory location 1048, and size of int is 4 bytes, then arr[0] volition be stored at 1048, arr[one] at 1052, arr[ii] at 1056 and arr[three] at 1060.
Any array is stored linearly in RAM.
However, in instance second arrays (or multidimensional arrays), there are conventions to decide the club of storing the elements in retentivity. The ii ways are:
- Row Major Gild
- Column Major Order
Note that elements will exist stored in face-to-face locations.
Row Major Gild
In row major social club, the elements of a particular row are stored at side by side memory locations. The kickoff chemical element of the array (arr[0][0]) is stored at the first location followed by the arr[0][1] and and so on. Afterward the first row, elements of the next row are stored next.
arr[3][three] =
[ a00, a01, a02 ]
[ b10, b11, b12 ]
[ c20, c21, c22 ]
Row major club = a00, a01, a02, b10, b11, b12, c20, c12, c22
If the first element is stored at memory location 1048 and the elements are integers, and so
- [1048] - a00
- [1052] - a01
- [1056] - a02
- [1060] - b10
- [1064] - b11
- [1068] - b12
- [1072] - c20
- [1076] - c21
- [1080] - c22
Column Major Order
In column major social club, the elements of a column are stored adjacent to each other in the memory.The kickoff element of the array (arr[0][0]) is stored at the first location followed by the arr[1][0] then on. After the beginning cavalcade, elements of the next column are stored stating from the pinnacle.
arr[3][3] =
[ a00, a01, a02 ]
[ b10, b11, b12 ]
[ c20, c21, c22 ]
Column major order = a00, b10, c20, a01, b11, c21, a02, b12, c22
If the first chemical element is stored at retention location 1048 and the elements are integers, then:
- [1048] - a00
- [1052] - b10
- [1056] - c20
- [1060] - a01
- [1064] - b11
- [1068] - c21
- [1072] - a02
- [1076] - b12
- [1080] - c22
Finding address of chemical element given the index
If we are given the address of the first element (This accost is also called the base accost) as well every bit the alphabetize of the chemical element, we tin observe out the accost of whatever element of the array. The method of finding the address is slightly unlike for 1D, 2D, and 3D arrays. We shall talk over each of them below.
1D Array
Given the base address I, and the array is of type Integer, then to calculate the accost of any element:
address[i] = I + i(sizeof (data blazon) - lower jump)*
More often than not, the indexing base is 0. We usually consider arrays that take 0 equally the first index. In some cases, arrays have 1 based indexing, which means that the outset alphabetize is 1.
Example:
Consider the base accost of an boolean array to be 1048. Find the address of the element at index = 5. (Indexing is 0 based)
address[5] = I + i*(sizeof(boolean) - lower bound)
address[5] = 1048 + v*(2) = 1048 + 10 = 1058
- 1048, 1049 = arr[0]
- 1050, 1051 = arr[ane]
- 1052, 1053 = arr[two]
- 1054, 1055 = arr[iii]
- 1056, 1057 = arr[iv]
- 1058, 1059 = arr[5]
2nd Array
- Row Major Address
The formula is intutive if we understand what it actually does. To calculate the address of an element in the ith row and jth column, we need to count how many memory locations accept been used by the elements in the preceeding i-i rows (where each row has N elements) in improver to the memory locations used past the preceeding j-ane elements in the current row. Each chemical element will crave as many bytes as used by the data type of the array. Hence, computing the number of bytes required past all the preceeding elements in a row major fashion and calculation this to the base of operations accost, would give utilise the address of the required element.
address[i][j] = I + W * (i - l_row) * N + (j - l_col)
I : Base of operations address
l_row : lower bound for row
l_col : lower bound for column
W : sizeof (data blazon)
N : Number of columns
Instance:
Consider an integer array of size 3X3. The accost of the first element is 1048. Calculate the address of the element at index i = ii, j = 1. (0 based alphabetize)
I = 1048, l_row = 0 = l_col, i = two, j = i, W = 2, Northward = 3
address[2][1] = I + West * (i-l_row) * North + (j - l_col)
address[2][1] = 1048 + 2 * ii * 3 + 1 = 1048 + 12 + 1 = 1061
- Column Major Accost
Here, for an element at index (i,j) we need to calculate the number of retention locations required past the elements in the preceeding j-1 columns (where each column has Chiliad elements) in add-on to the i-1 elements in the current column. Adding this amount to the base chemical element volition give us the accost of the required element.
address[i][j] = I + West * ((j – l_col) * M + (i – l_row))
I : Base address
l_row : lower spring for row
l_col : lower bound for column
W : sizeof (data type)
Thou : Number of rows
Example:
Consider an integer assortment of size 3X3. The address of the first element is 1048. Calculate the address of the chemical element at index i = two, j = 1. (0 based alphabetize)
I = 1048, l_row = 0 = l_col, i = 2, j = one, West = two, M = iii
address[2][1] = I + W * (j - l_col) * M + (i - l_row)
address[2][i] = 1048 + ii * i * 3 + 2 = 1048 + vi + 2 = 1056
3D Array
- Row Major Order
address of[i][j][k] = I + Westward * {[(i – l_row) * N] + [(j – l_col)]} * R + [yard – l_block]
I : Base address,
W : sizeof (data blazon) in bytes
l_row : lower spring for row
l_col : lower jump for cavalcade
l_block : lower bound for block
Due north : Number of columns
R : Number of blocks
- Column Major Order
address of[i][j][k] = I + W * {[(i – l_row)] + [(j – l_col) * M]} * R + [1000 – l_block]
I : Base accost,
Westward : sizeof (information type) in bytes
l_row : lower spring for row
l_col : lower bound for cavalcade
l_block : lower jump for block
N : Number of columns
R : Number of blocks
Comparing of Row Major Order VS Column Major Order
Storing elements in row major order matrix improves the performance when the array elements are to be traversed in a contiguous mode. This means traversing the array in a way that the elements of the starting time row are traversed first and then the elements of the side by side row and then on. Row major guild becomes a improve choice in such cases because elements are stored exactly like this in memory and hence the traversal would simply hateful moving through contiguous memory locations.
Column Major Order would exist more useful in case the traversal involves going through the elements in the same column first and so onto the next i. This is intuitively a amend approach as the traversal would then require movinf through contiguous retention location.
All in all, the advantage is entirely performance based which might vary depending on the utilise instance. Merely Row major order might generally yield better functioning because the cache prefetches contiguous elements which are used in instance of row major gild. However, in example of column major lodge the cache prefetch is not used because the elements in the cahe are the elements in same row but for column major lodge the elements from the same column need to exist traversed.
How to decide if elements are stored in row major or column major social club?
A lot depends on the language we are using. For example, FORTRAN stores the elements in Cavalcade Major Society whereas C/C++ stores the elements in Row Major Club.
Python on the other hand enables the programmer to specify the order. We tin can use both, row and column major order, in the same programme.
With this article at OpenGenus, y'all must have the complete idea of Row major and Column major order.
0 Response to "How to Print Out Contents of an Array in Row Major Fashion C"
Post a Comment