Two-Dimensional Arrays and Address Calculation (ISC Class 12 Computer Science)
Updated 17 October 2026 · Draft, not yet fact-checked
For A[l1..u1][l2..u2] with element size w and base address B, row-major address of A[i][j] is B + w x [(i - l1) x N + (j - l2)] with N = u2 - l2 + 1 columns; column-major is B + w x [(j - l2) x M + (i - l1)] with M = u1 - l1 + 1 rows. Always count rows and columns from the bounds.
Memory is one line: row-major stores each row together, column-major stores each column together, so the count of elements before A[i][j] differs.
Illustration drawn for QuestionBuddy.
Understand Two-Dimensional Arrays and Address Calculation
Two-dimensional array in Java. int[][] m = new int[3][4]; makes 3 rows and 4 columns (12 elements). m.length is the number of rows and m[0].length the number of columns. Indices start at 0. A matrix is processed with nested loops, the outer one over rows. For a square matrix the principal diagonal is m[i][i] and the other diagonal is m[i][n-1-i].
Storage in memory. Memory is one long line, so a 2-D array is stored in one of two orders:
- Row-major: row by row (all of row 1, then row 2, ...). Java and C store arrays this way.
- Column-major: column by column.
Address formulas. Let the array be A[l1..u1][l2..u2] (rows from l1 to u1, columns from l2 to u2), each element take w bytes, and the base address (address of A[l1][l2]) be B.
- Rows M = u1 - l1 + 1 and columns N = u2 - l2 + 1.
- Row-major: address of A[i][j] = B + w x [(i - l1) x N + (j - l2)].
- Column-major: address of A[i][j] = B + w x [(j - l2) x M + (i - l1)].
- When both lower bounds are 0 these become B + w(iN + j) and B + w(jM + i).
The bracket counts how many elements come before A[i][j]. Bounds may be negative, so use subtraction carefully: i - l1 with l1 = -3 and i = 2 is 5.
Steps for a numerical question. (1) Find M and N. (2) Choose the formula for the stated order. (3) Find the number of elements before the target. (4) Multiply by w and add B. (5) Check that the answer lies within B to B + w x M x N.
Sorting and searching 1-D arrays are in the previous topic; strings are in the next topic.
Two-Dimensional Arrays and Address Calculation: key facts
- Row-major address
- Address = B + w x [(i - l1) x N + (j - l2)]
- N = u2 - l2 + 1 columns
- Elements are stored row by row
- B is the address of A[l1][l2].
- Column-major address
- Address = B + w x [(j - l2) x M + (i - l1)]
- M = u1 - l1 + 1 rows
- Elements are stored column by column
- Swap the roles of rows and columns.
- Size of the array
- Elements = M x N
- Bytes = M x N x w
- Last element is at index M x N - 1 from the base
- Use it to check the answer.
- Java matrix facts
- new int[r][c] has r x c elements
- m.length = rows
- m[0].length = columns
- Diagonals: m[i][i] and m[i][n-1-i]
- Indices start at 0.
How to answer Two-Dimensional Arrays and Address Calculation questions
Use this routine for address calculation.
- 1Write the bounds l1, u1, l2, u2 and the element size w and the base B.
- 2Calculate M = u1 - l1 + 1 and N = u2 - l2 + 1.
- 3Write the formula for row-major or column-major as asked, with the numbers substituted.
- 4Compute the number of elements before the target, then multiply by w.
- 5Add the base address and give the units if mentioned.
- 6Check the result lies inside the array and compare with the other order if asked.
Two-Dimensional Arrays and Address Calculation in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Seat map in a hall
Suppose a hall has seats numbered by row 1 to 6 and seat 1 to 8 and a program stores the seat flags row by row starting at address 2000, two bytes per seat.
Find the address of row 4, seat 5.
Try it first, then show the solutionSolution
- N = 8, elements before it = (4 - 1) x 8 + (5 - 1) = 28.
- Address = 2000 + 2 x 28 = 2056.
Answer: 2056
Memory hook: Count the whole rows above, then the seats along the row.
Same cell, two storage orders
Suppose the same hall data is stored column by column instead.
What is the address of row 4, seat 5 now?
Try it first, then show the solutionSolution
- M = 6 rows. Elements before it = (5 - 1) x 6 + (4 - 1) = 27.
- Address = 2000 + 2 x 27 = 2054.
Answer: 2054
Memory hook: Whole columns first, then down the column.
Tricks to remember Two-Dimensional Arrays and Address Calculation
Short memory aids. Say each one aloud once, then use it on a question the same day.
Row-major = rows times columns-count
Skip complete rows: (i - l1) times N, then walk j - l2 along the row.
Use it when: Row-major formula.
Column-major = columns times rows-count
Skip complete columns: (j - l2) times M, then walk i - l1 down the column.
Use it when: Column-major formula.
Count from the lower bound
Every index is measured from its lower bound, never from zero.
Use it when: Non-zero or negative bounds.
N is columns, M is rows
N for the number of columns that makes a row long; M for the number of rows.
Use it when: Choosing M and N.
Two-Dimensional Arrays and Address Calculation: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Storage order · 1 mark · MCQBoard level
In row-major storage of a two-dimensional array,
(a) elements of a column are adjacent (b) elements of a row are adjacent (c) elements are in random order (d) only the diagonal is stored
Try it first, then show the solutionSolution
- Row-major stores row after row, so neighbouring elements of one row are adjacent in memory.
Answer: (b) elements of a row are adjacent
Example 2: Both orders with lower bound 1 · 4 marks · Short answerHarder board level
An array A[1..5][1..4] stores 2-byte elements with base address 1000. Find the address of A[3][2] in (a) row-major and (b) column-major order.
Try it first, then show the solutionSolution
- M = 5 and N = 4. (0.5 mark)
- Row-major: elements before = (3 - 1) x 4 + (2 - 1) = 9; address = 1000 + 2 x 9 = 1018. (1.5 marks)
- Column-major: elements before = (2 - 1) x 5 + (3 - 1) = 7; address = 1000 + 2 x 7 = 1014. (1.5 marks)
- Both lie inside 1000 to 1040 (20 elements x 2 bytes). (0.5 mark)
Answer: Row-major 1018; column-major 1014
Example 3: Negative lower bound · 4 marks · Short answerHarder board level
M[-3..4][2..6] stores 4-byte elements; the base address is 500. Find the address of M[2][5] in row-major and column-major order.
Try it first, then show the solutionSolution
- Rows M = 4 - (-3) + 1 = 8; columns N = 6 - 2 + 1 = 5. (1 mark)
- Row-major: (2 - (-3)) x 5 + (5 - 2) = 25 + 3 = 28 elements before; 500 + 4 x 28 = 612. (1.5 marks)
- Column-major: (5 - 2) x 8 + (2 - (-3)) = 24 + 5 = 29 elements before; 500 + 4 x 29 = 616. (1.5 marks)
Answer: Row-major 612; column-major 616
Example 4: Matrix diagonals and a row · 3 marks · Short answerHarder board level
Write the output:
int[][] m = {{2, 7, 1}, {4, 5, 9}, {3, 8, 6}};
int d = 0, a = 0, r = 0;
for (int i = 0; i < 3; i++) {
d += m[i][i];
a += m[i][2 - i];
r += m[1][i];
}
System.out.println(d + " " + a + " " + r);Try it first, then show the solutionSolution
- Principal diagonal: 2 + 5 + 6 = 13. (1 mark)
- Other diagonal m[0][2], m[1][1], m[2][0]: 1 + 5 + 3 = 9. (1 mark)
- Row 1 (the second row): 4 + 5 + 9 = 18. Output: 13 9 18. (1 mark)
Answer: 13 9 18
Example 5: Case: seat map storage · 4 marks · Case-basedHOTS / competency
Suppose a hall program keeps flags in A[1..6][1..8] with 2 bytes per seat and base address 2000.
(i) Find the address of seat (4, 5) in row-major and in column-major order. (2 marks)
(ii) Find the total bytes used. (1 mark)
(iii) Show that seat (6, 8) has the same address in both orders. (1 mark)
Try it first, then show the solutionSolution
- (i) Row-major: (4 - 1) x 8 + (5 - 1) = 28, so 2000 + 56 = 2056. Column-major: (5 - 1) x 6 + (4 - 1) = 27, so 2000 + 54 = 2054.
- (ii) 6 x 8 = 48 seats, so 96 bytes.
- (iii) Row-major: 5 x 8 + 7 = 47. Column-major: 7 x 6 + 5 = 47. Both 47, so the address is 2000 + 94 = 2094. The last element is the same in both orders.
Answer: (i) 2056 and 2054 (ii) 96 bytes (iii) both orders give element number 47, address 2094
Where students lose marks in Two-Dimensional Arrays and Address Calculation
The slips examiners see most often, and what to write instead.
Mistake: Using the wrong bound for N or M
N is the number of columns, M the number of rows; a swap changes the answer.
Wrong working
Row-major with M instead of N
Right working
Row-major uses N (columns); column-major uses M (rows)
Do this: Write M and N from the bounds first.
Usually costs: 1 mark
Mistake: Forgetting the lower bound
Positions are counted from the lower bound, not from zero.
Wrong working
A[1..5][1..4], element A[3][2]: (3 x 4 + 2)
Right working
(3 - 1) x 4 + (2 - 1) = 9
Do this: Subtract l1 and l2 every time.
Usually costs: 1 mark
Mistake: Using u - l instead of u - l + 1
The number of values from l to u includes both ends.
Wrong working
N = 6 - 2 = 4 for columns 2 to 6
Right working
N = 6 - 2 + 1 = 5
Do this: Always add 1.
Usually costs: 1 mark
Exam tips
- Write the formula with symbols first, then substitute; method marks are given.
- Show M and N calculations on separate lines.
- State the order (row-major or column-major) in your answer.
- Check the address lies between the base and the last element.
- For Java matrix programs, mention rows as the first index and columns as the second.
Practice questions on Two-Dimensional Arrays and Address Calculation
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Elements stored column after column are said to be in
(a) row-major order (b) column-major order (c) sorted order (d) heap orderHint
Named after the unit that is stored together.
Answer
(b) column-major order
Q2 · 2 marks · Short answerBoard level
For int[][] b = new int[4][3]; write b.length, b[0].length and the total number of elements.
Hint
First size is rows.
Answer
b.length = 4 (rows), b[0].length = 3 (columns), total 4 x 3 = 12 elements.
Q3 · 4 marks · Short answerHarder board level
An array A[0..9][0..7] stores 4-byte elements with base address 3000. Find the address of A[6][5] in row-major and column-major order.
Hint
M = 10, N = 8.
Answer
M = 10, N = 8. Row-major: 6 x 8 + 5 = 53 elements before, so 3000 + 4 x 53 = 3212. Column-major: 5 x 10 + 6 = 56 elements before, so 3000 + 4 x 56 = 3224.
Q4 · 4 marks · Short answerHarder board level
M[-2..3][-1..2] stores 8-byte elements with base address 100. Find the address of M[1][0] in row-major and column-major order.
Hint
Rows = 6, columns = 4.
Answer
M = 3 - (-2) + 1 = 6 rows; N = 2 - (-1) + 1 = 4 columns. Row-major: (1 + 2) x 4 + (0 + 1) = 13 elements before; 100 + 8 x 13 = 204. Column-major: (0 + 1) x 6 + (1 + 2) = 9; 100 + 8 x 9 = 172.
Q5 · 4 marks · Case-basedHOTS / competency
A program transposes a 2 x 3 matrix:
int[][] a = {{1, 2, 3}, {4, 5, 6}}; int[][] t = new int[3][2]; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) t[j][i] = a[i][j]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.print(t[i][j] + " "); System.out.println(); }(i) Write the output. (ii) Why must t be declared as [3][2] and not [2][3]?
Hint
Rows become columns.
Answer
(i) The output lines are:
1 4
2 5
3 6
(ii) Transposing turns the 2 rows and 3 columns of a into 3 rows and 2 columns of t; with [2][3] the assignment t[j][i] with j up to 2 would go out of bounds and throw ArrayIndexOutOfBoundsException.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What is the row-major formula?Show answer
B + w x [(i - l1) x N + (j - l2)].
2.What is the column-major formula?Show answer
B + w x [(j - l2) x M + (i - l1)].
3.How many columns does A[2..7][3..9] have?Show answer
7.
4.How does Java store a 2-D array?Show answer
As rows (row-major in practice).
5.How do you get the number of rows of m in Java?Show answer
m.length.
6.Which element of a 3 x 3 matrix is on the other diagonal in row i?Show answer
m[i][2 - i].
Two-Dimensional Arrays and Address Calculation: frequently asked questions
What is the difference between row-major and column-major order?
Row-major stores one complete row after another; column-major stores one complete column after another.
How is the address of an element in a 2-D array calculated?
Add to the base address the element size times the number of elements stored before it, found with the row-major or column-major formula using the bounds.
What if the lower bound is negative?
Use the same formulas; i - l1 is then i plus the absolute value of l1, so M[-3..4] with i = 2 gives 5 rows before.
How do I find the number of columns in a Java 2-D array?
Use the length of the first row, for example m[0].length.