Row Major and Column Major Address Calculation for GATE: 2D Array Numericals Solved

Derive both 2D array address formulas from a flat memory block. The same element is solved in row-major and column-major order for 0-based and 1-based indexing.

KnowledgeGate Team

Exam prep & CS education

Updated 29 Aug 20265 min read56 views

A 2D array looks like a grid in a program, but memory stores it as one continuous sequence of elements. Address questions only ask how many elements appear before A[i][j] in that sequence. Decide the storage order, lower bounds and element size first, and the arithmetic becomes routine.

An array is a flat block of memory

Suppose an array has rows and columns. Its elements occupy consecutive memory locations, so the address of any element has the form:

address = base address + offset in elements x size of one element

The base address is the address of the element at the declared lower bounds. The offset counts how many elements come before the requested element. Multiplying that offset by the element size converts an element count into a byte count.

The difference between the two layouts is the order used to flatten the grid:

  • Row-major order stores the complete first row, then the complete second row, and so on. C uses this order for multidimensional arrays.

  • Column-major order stores the complete first column, then the complete second column, and so on. Fortran is the standard language example.

Before substituting any number, write down the number of rows, number of columns, row and column lower bounds, element size and stated storage order.

Row-major and column-major formulas

Let a 2D array have N1 rows and N2 columns. With 0-based indices, row-major order places N2 complete elements for every earlier row, followed by j elements in the current row:

row-major address(A[i][j]) = base + (i x N2 + j) x size

Column-major order places N1 complete elements for every earlier column, followed by i elements in the current column:

column-major address(A[i][j]) = base + (j x N1 + i) x size

For lower bounds lb1 on rows and lb2 on columns, shift the indices before applying the same logic:

row-major offset    = (i - lb1) x N2 + (j - lb2)
column-major offset = (j - lb2) x N1 + (i - lb1)

A useful verbal check is that row major weights the row index by the number of columns. Column major weights the column index by the number of rows.

Worked example in both orders with 0-based indexing

Given A[10][20], take:

  • 10 rows and 20 columns

  • base address 1000

  • element size 4 bytes

  • indices starting at 0

  • required element A[4][5]

For row-major order, four complete rows appear first. Each contains 20 elements, followed by five elements from row 4 before column 5:

offset = i x N2 + j
       = 4 x 20 + 5
       = 80 + 5
       = 85 elements

address = 1000 + 85 x 4
        = 1000 + 340
        = 1340

For column-major order, five complete columns appear first. Each contains 10 elements, followed by four elements from column 5 before row 4:

offset = j x N1 + i
       = 5 x 10 + 4
       = 50 + 4
       = 54 elements

address = 1000 + 54 x 4
        = 1000 + 216
        = 1216

The logical element is the same, but its addresses are 1340 and 1216 because its positions in the two flattened sequences are different.

A 10 by 20 grid marks A[4][5] beside row-major and column-major memory strips showing offsets 85 and 54 at addresses 1340 and 1216.

The 1-based variant and 3D generalisation

Now declare the same shape as A[1..10][1..20]. Keep base address 1000, element size 4 bytes and requested element A[4][5]. The base now belongs to A[1][1], so subtract 1 from both indices.

For row-major order:

offset = (4 - 1) x 20 + (5 - 1)
       = 3 x 20 + 4
       = 64 elements

address = 1000 + 64 x 4
        = 1000 + 256
        = 1256

For column-major order:

offset = (5 - 1) x 10 + (4 - 1)
       = 4 x 10 + 3
       = 43 elements

address = 1000 + 43 x 4
        = 1000 + 172
        = 1172

The lower bounds changed the offsets from 85 and 54 to 64 and 43. That is why using the formula before reading the declaration is risky.

For a 0-based 3D array A[i][j][k] with dimension sizes D1, D2 and D3, the same flattening rule gives both offsets:

row-major offset    = (i x D2 + j) x D3 + k
column-major offset = (k x D2 + j) x D1 + i

The last index varies fastest in row major, while the first index varies fastest in column major. For non-zero lower bounds, replace i, j and k with i - lb1, j - lb2 and k - lb3 before evaluating the appropriate expression.

Traps GATE plants in address numericals

The most common mistake is swapping the dimension multiplier. Row major uses the number of columns, while column major uses the number of rows. Sketching two rows or columns is often enough to catch the swap.

Next, separate element offset from byte offset. If the question asks for the number of elements before A[i][j], stop after computing the offset. If it asks for an address, multiply by the stated element size and then add the base.

Never assume 0-based indexing merely because the notation uses square brackets. Read the declared lower bounds. Also use the element size supplied in the question rather than relying on a language or machine assumption.

In row major, the last index changes fastest. In column major, the first index changes fastest. This gives you a quick check when a question presents a sequence of neighbouring addresses.

How GATE tests array address calculation

GATE can ask for an element address, recover the base address from one known element, infer a dimension from the difference between two addresses, or extend the same logic to three dimensions. The layout becomes easier to see after Pointers in C for GATE, while base-plus-index hardware calculations connect to Addressing modes and instruction formats in computer architecture explained.

When two element addresses are known, subtract them first. The base cancels, leaving an equation in the unknown dimension or element size. For any current subject weightage or paper pattern, use the official GATE portal of the organising IIT; the address formulas themselves do not change.

Short version and next step

Start with address = base + offset x size. In row major, compute (i - lb1) x number of columns + (j - lb2). In column major, compute (j - lb2) x number of rows + (i - lb1).

Drill the variations in the GATE Test Series, build the array-memory idea through GATE Guidance by Sanchit Sir, and use the GATE preparation category to connect it to pointers and addressing.