1-D Array Base Address Formula
A computer does not "find" the element you wrote as A[5] โ it computes the memory address with a formula and jumps there in one step. Master that one formula, and every multidimensional and pointer question in GATE becomes trivial; misread it, and you lose three easy marks every year.
Definition: A one-dimensional array is a sequence of equally-sized memory cells stored contiguously, accessed by an integer index.
Definition: The base address of an array is the memory address of its first element. The word size w is the number of bytes each element occupies.
The Master Formula
For a 1-D array A declared with lower bound L, the address of element A[i] is:
Address(A[i]) = Base + (i โ L) ร w
where Base is the address of A[L] (the first element), and w is the size of each element in bytes. The formula has only two ideas: (i โ L) is the number of slots from the start, and multiplying by w converts slot count into bytes.
In C, all arrays start at index 0, so L = 0, and the formula collapses to the familiar form:
Address(A[i]) = Base + i ร w
In Pascal, FORTRAN, or any language where you can declare an array as A[5..15], L is non-zero, and you must subtract it. This subtraction is the single most common source of GATE errors.
Why Subtract L?
Imagine A is declared from index 10 to 20. The first cell is A[10], not A[0]. If you ask for A[12], you are at the third cell โ that is, two slots past the base, not twelve. The (i โ L) term shifts your thinking so that the first element is always at offset zero, regardless of how the programmer chose to label the indices. This is what makes the formula language-agnostic.
Why it matters: GATE consistently asks "given Base = 1000, element size 4 bytes, lower bound 5, find the address of A[10]." A student who applies the formula gets it in 5 seconds: 1000 + (10 โ 5) ร 4 = 1000 + 20 = 1020. A student who forgets to subtract 5 gets 1040 โ a confident wrong answer. Examiners design the distractors around exactly this mistake.
Real-world example: Consider a 1-D int array int A[100] in C running on a 64-bit machine. With sizeof(int) = 4 bytes and base address 0x7ffd1000, A[37] sits at 0x7ffd1000 + 37 ร 4 = 0x7ffd1094. The C compiler emits a single lea (load effective address) instruction that computes precisely this expression at runtime. The formula is not just exam fodder โ it is the physical machine operation.
Common misconception: "The address of A[i] is Base + i ร w in every language." False. The shortcut Base + i ร w only works when L = 0. The general formula is always Base + (i โ L) ร w. GATE has tested this distinction in multiple years (notably with arrays declared as A[โ5..5] or A[1..10]).
Worked Example โ Non-Zero Lower Bound
Question: A 1-D array A is declared as A[5..50] with each element occupying 4 bytes. If the base address is 1000, find the address of A[30].
Solution:
Step 1: Identify the parameters. Base = 1000, L = 5, i = 30, w = 4.
Step 2: Apply the master formula: Address(A[30]) = Base + (i โ L) ร w = 1000 + (30 โ 5) ร 4.
Step 3: Compute (i โ L) = 25. This is the count of cells between A[5] and A[30].
Step 4: Multiply by w = 4 bytes per cell: 25 ร 4 = 100 bytes of offset.
Step 5: Add to base: 1000 + 100 = 1100.
Conclusion: A[30] is at address 1100. Verification: there are 25 cells before A[30] starting from A[5], each occupying 4 bytes, so the offset is 25 ร 4 = 100, matching.
Total Memory Occupied
For an array of n elements, each occupying w bytes, the total memory is simply n ร w bytes. If A is declared as A[L..U], then n = U โ L + 1 (don't forget the +1 โ both endpoints are included). For A[5..50], n = 50 โ 5 + 1 = 46 cells, occupying 46 ร 4 = 184 bytes.
The "+1 trap" is another favourite of paper-setters. Always remember: number of elements from L to U inclusive equals U โ L + 1, not U โ L.
Why This Underpins Higher Dimensions
The 1-D formula is the seed. The 2-D formula in row-major order for an array A[m][n] is:
Address(A[i][j]) = Base + ((i โ Lโ) ร n + (j โ Lโ)) ร w
and in column-major order:
Address(A[i][j]) = Base + ((j โ Lโ) ร m + (i โ Lโ)) ร w
Both reduce to repeated application of "count slots, multiply by width." If you cannot do the 1-D formula in your sleep, the 2-D and 3-D versions become a nightmare. So practise the 1-D drill until it feels automatic.
A Subtle Pointer-Arithmetic Twist
In C, the expression A + i is not equal to (char *)A + i. The compiler scales i by sizeof(*A) automatically. So if A is int *, then A + 1 is 4 bytes ahead of A, not 1 byte. GATE has framed questions where you must decide whether the offset was already scaled or whether you must scale it yourself. The rule is: typed pointer arithmetic scales automatically; raw byte arithmetic does not.
| Quantity | Formula (general) | Formula in C (L = 0) |
|---|---|---|
| Address of A[i] | Base + (i โ L) ร w | Base + i ร w |
| Number of elements from L to U | U โ L + 1 | n |
| Total bytes | (U โ L + 1) ร w | n ร w |
| Offset of A[i] from base | (i โ L) ร w | i ร w |
- โ- Master formula: Address(A[i]) = Base + (i โ L) ร w.
- โ- In C arrays L = 0 always; the formula simplifies to Base + i ร w.
- โ- Always subtract the lower bound before scaling by the element size.
- โ- Number of elements from L to U inclusive is U โ L + 1, not U โ L.
- โ- Total memory of n-element array = n ร w bytes.
- โ- Typed pointer arithmetic in C scales offsets by sizeof(*p) automatically.
- โ- The 1-D formula is the foundation for all row-major and column-major 2-D addressing.
"Count the gap from the lower bound, scale by the width." Whenever you see A[i], whisper "i minus L, times w, plus base."
- โ- The address formula is a two-step machine: compute slot offset, then scale by element size.
- โ- Subtracting the lower bound L is non-negotiable for arrays not starting at index zero.
- โ- Off-by-one errors come from forgetting the +1 when counting elements between two indices.
- โ- All higher-dimensional addressing extends this same idea โ perfect the 1-D version first.
2-D Array: Row-Major vs Column-Major
Some numbers refuse to fit into the neat p/q box, no matter how clever your algebra. These restless numbers โ root 2, root 3, root 5, pi โ are called irrational, and proving they are irrational is one of the most elegant exercises in your NCERT Class 10 chapter.
Definition: A rational number is any number that can be written in the form p/q, where p and q are integers and q is not zero. Examples include 3, -7/4, 0, and 0.25 (which is 1/4).
Definition: An irrational number is any real number that cannot be written as p/q with integers p and q. Its decimal expansion is non-terminating and non-repeating. Examples include the square root of 2, the square root of 3, the square root of 5, and pi.
A key theorem we will lean on
Before any irrationality proof, we recall this theorem (a consequence of the Fundamental Theorem of Arithmetic, which says every integer greater than 1 has a unique prime factorisation):
If p is a prime number and p divides a^2 (where a is a positive integer), then p also divides a.
The intuition is simple. When you square a number, every prime in its factorisation appears with an even exponent. So if a prime p shows up inside a^2, it must already have been inside a โ at least once. We use this fact like a hammer in every proof that follows.
Method: proof by contradiction
To prove a number is irrational, mathematicians use the classic strategy called proof by contradiction. The steps are:
- Assume the opposite โ pretend the number IS rational, so it can be written as p/q.
- Insist that the fraction is in lowest terms, meaning the only common factor of p and q is 1. (Every rational can be reduced to lowest terms, so this costs us nothing.)
- Do honest algebra. At some point you will discover that p and q share a prime factor after all.
- That contradicts the lowest-terms assumption. So our original assumption was false, and the number must be irrational.
This idea is centuries old but still feels magical: we prove a number is not rational by pretending it is and watching the pretence collapse.
Worked example โ root 3 is irrational
Question: Prove that the square root of 3 is irrational.
Solution:
Step 1: Assume, for contradiction, that root 3 is rational. Then root 3 = p/q for some integers p, q with q non-zero, and the fraction p/q is in lowest terms (gcd(p, q) = 1).
Step 2: Square both sides: 3 = p^2 / q^2, so 3 q^2 = p^2. This means 3 divides p^2.
Step 3: By the key theorem (with p = 3), since 3 is prime and 3 divides p^2, it follows that 3 divides p. Write p = 3m for some integer m.
Step 4: Substitute back: 3 q^2 = (3m)^2 = 9 m^2, so q^2 = 3 m^2. This means 3 divides q^2, and again by the theorem, 3 divides q.
Step 5: But now 3 is a common factor of p and q โ contradicting our lowest-terms assumption in Step 1.
Conclusion: Our assumption was false. Hence root 3 is irrational.
The exact same template proves that root 2, root 5, root 7 and every root of a prime is irrational. NCERT chapter 1 asks for at least one such proof โ examiners may pick any prime.
Why it matters
Irrational numbers complete the real number line. Without them, the line would be full of microscopic holes โ the point on a 1-by-1 square's diagonal (length root 2) would have no number to label it. The Class 10 board exam tests three abilities: stating what irrational means, executing the proof by contradiction cleanly, and combining rational and irrational numbers under the closure rules. Mastery here also feeds directly into surds, logarithms and the completeness of real numbers that you will meet in Class 11.
Real-world example
Pi (pi), the ratio of a circle's circumference to its diameter, is irrational. That is why your school calculator can only ever approximate the area of a circle โ every digit of pi after the decimal is a fresh, unrepeating value. When the Mangalyaan probe was navigated to Mars, ISRO engineers carried pi to dozens of decimal places because every truncation introduces error. The irrationality of pi is the reason that "exact" trajectory work always uses the symbol pi instead of a decimal.
Common misconception
The most common mistake is forgetting to state that p/q is in lowest terms at the very start. Without that condition, finding that 3 divides both p and q does not contradict anything โ the fraction simply was not in simplest form. Another error: claiming a number is irrational "because it looks messy" or "because the calculator does not show a pattern." Looks can deceive โ 1/7 = 0.142857142857... looks endless on screen, but it actually repeats and is perfectly rational.
Useful closure facts
These three facts get tested in MCQ form:
- Rational + irrational = irrational. (Example: 2 + root 3 is irrational.)
- A non-zero rational times an irrational = irrational. (Example: 5 root 2 is irrational.)
- Sum of two irrationals may be rational or irrational. (Example: root 2 + (-root 2) = 0, which is rational.)
| Property | Rational numbers | Irrational numbers |
|---|---|---|
| Form | Can be written as p/q with q non-zero | Cannot be written as p/q |
| Decimal expansion | Terminating or non-terminating but repeating | Non-terminating and non-repeating |
| Examples | 1/2, -3, 0.75, 0.333... | root 2, root 3, pi, e |
| Closure under + | Closed | Not closed |
| Closure under x | Closed | Not closed |
- โ- An irrational number cannot be written as p/q where p, q are integers and q is non-zero.
- โ- The decimal expansion of an irrational number is non-terminating and non-repeating.
- โ- To prove a number is irrational, assume it is rational in lowest terms, then derive a contradiction.
- โ- Key theorem: if a prime p divides a^2, then p divides a.
- โ- Forgetting the lowest terms condition is the most common proof error.
- โ- Rational + irrational is always irrational; non-zero rational times irrational is irrational.
- โ- Square roots of all primes (2, 3, 5, 7, 11, ...) are irrational by the same template.
"ALAS" โ Assume rational, write in Lowest terms, do Algebra, find Shared factor (contradiction). Each letter is a step in the proof template. Once you hear "prove irrational", the word ALAS should reach for the chalk for you.
- โ- Rational means expressible as p/q; irrational means it cannot be expressed that way.
- โ- Proof by contradiction is the standard NCERT method.
- โ- The proof always ends by showing p and q share a prime factor โ contradicting lowest terms.
- โ- Pi, root 2, root 3, root 5 are the standard irrational examples to remember.
Worked Example: 2-D Row-Major Address
"Find the molecular formula" is one of the most reliable easy-marks questions in NEET Chemistry. The path from a percentage analysis to the actual molecular formula is always the same four steps; once you internalise the algorithm, you can solve these in under a minute.
Definition: Empirical formula is the simplest whole-number ratio of atoms in a compound.
Definition: Molecular formula is the actual number of atoms of each element in one molecule; it is always an integer multiple of the empirical formula, where the multiplier n = molar mass / empirical mass.
Setting up the problem
We are given a compound that, on combustion analysis, was found to contain 40% C, 6.7% H, 53.3% O by mass. Its molar mass, measured separately (say by vapour density or mass spectrometry), is 60 g/mol. We are asked: what is the molecular formula?
The trick that makes percentages painless is to assume you have exactly 100 g of the compound. Then the percentages become grams directly, and you avoid carrying "%" through every line.
The four-step algorithm
Step 1: Convert mass percentages to moles.
Step 2: Divide every mole value by the smallest one to get the simplest ratio.
Step 3: Write the empirical formula and compute its empirical mass.
Step 4: Find n = (molar mass) / (empirical mass); multiply subscripts by n.
That is the entire method. Let us walk through it carefully.
Worked example โ full execution
Question: A compound contains 40 % C, 6.7 % H, 53.3 % O by mass. Its molar mass is 60 g/mol. Find the molecular formula.
Solution:
Step 1: Assume 100 g of the compound, so we have 40 g of C, 6.7 g of H and 53.3 g of O.
Convert to moles using atomic masses (C = 12, H = 1, O = 16):
- Moles of C = 40 / 12 = 3.33
- Moles of H = 6.7 / 1 = 6.7
- Moles of O = 53.3 / 16 = 3.33
Step 2: Divide by the smallest mole value (3.33):
- C : 3.33 / 3.33 = 1
- H : 6.7 / 3.33 = 2.01 โ 2
- O : 3.33 / 3.33 = 1
Step 3: The simplest whole-number ratio is C : H : O = 1 : 2 : 1, so the empirical formula is CHโO. Its empirical mass = 12 + 2(1) + 16 = 30 g/mol.
Step 4: Find n = molar mass / empirical mass = 60 / 30 = 2. Multiply each subscript of the empirical formula by 2 to get the molecular formula CโHโOโ.
Conclusion: The compound is CโHโOโ โ acetic acid (CHโCOOH).
Why this algorithm works (the WHY)
Percentages by mass tell us the mass of each element in a fixed amount of compound. Dividing by atomic mass converts mass to moles, and moles are the same thing as "number of atoms" up to Avogadro's constant. So the mole ratio is the atom ratio. Dividing by the smallest mole value rescales that ratio so the smallest term is 1, which is the cleanest way to read off small integer subscripts.
The factor n comes from the fact that the empirical formula carries only the ratio; it loses the absolute count. The measured molar mass restores that count.
Why it matters
Why it matters: This kind of question appears almost every year in NEET, JEE Main and CBSE Class 11 boards. It is also the conceptual foundation for combustion analysis, percent purity problems, and the elemental analysis component of organic chemistry. If you can do this confidently, you will pick up 4 easy marks that many students lose to silly arithmetic.
Real-world example
Real-world example: The compound we just identified โ acetic acid โ is the active ingredient in vinegar (about 5 % by mass in table vinegar). When a food chemist sends an unknown organic acid for elemental analysis and gets back 40 / 6.7 / 53.3 for C / H / O with molar mass 60, this exact calculation is how they conclude "yes, this is acetic acid." The same method, with nitrogen included, is how forensic labs identify drug compounds.
Handling messy ratios
If after Step 2 you get values like 1, 1.5 โ multiply every term by 2 (giving 2, 3). If you get 1, 1.33 or 1, 1.67 โ multiply by 3 (giving 3, 4 or 3, 5). The tip in your fragment is the key rule:
- ratio โ 0.33 or 0.67 โ multiply by 3
- ratio โ 0.5 โ multiply by 2
- ratio โ 0.25 or 0.75 โ multiply by 4
- ratio โ 0.2 โ multiply by 5
Do not round 1.5 down to 1 or up to 2 โ that gives the wrong formula. Multiply instead.
Always verify
Always verify: once you have your molecular formula, compute its molar mass back from atomic masses and check it matches the given molar mass.
For CโHโOโ: 2(12) + 4(1) + 2(16) = 24 + 4 + 32 = 60 g/mol. โ Matches.
This 10-second check has saved thousands of students from a careless subscript error.
Common misconception
Common misconception: "Empirical formula and molecular formula are the same thing." They coincide only when n = 1. For water (HโO, molar mass 18) both are HโO. For glucose, the empirical is CHโO (mass 30) but the molecular is CโHโโOโ (mass 180, so n = 6). And our acetic-acid example also has empirical CHโO โ three different molecules can share an empirical formula. You need the molar mass to distinguish them.
Another common error: forgetting that the percentages must add to 100 %. If C + H = 46.7 % and no oxygen is mentioned, assume the remaining 53.3 % is the oxygen (or whichever element is implied by the chemistry). NEET sometimes tests this โ read carefully.
| Step | What you do | Why |
|---|---|---|
| 1 | mass % โ grams (assume 100 g) โ moles | Convert to atom counts |
| 2 | Divide all moles by smallest | Get simplest ratio |
| 3 | Whole-number ratio โ empirical formula | Lose absolute count, keep ratio |
| 4 | n = M(actual) / M(empirical); multiply subscripts | Restore absolute count |
- โ- Assume 100 g of compound โ % becomes grams instantly.
- โ- moles_i = mass_i / atomic_mass_i.
- โ- Divide every mole value by the smallest to get the atom ratio.
- โ- If ratios aren't whole numbers, multiply (ร2 for .5, ร3 for .33/.67, etc.). Never round!
- โ- n = molar_mass / empirical_mass. Multiply subscripts by n.
- โ- Verify by recomputing the molar mass of your final formula.
- โ- Empirical formula is unique up to a positive integer multiplier; molar mass picks the right one.
MMRM: Mass-percent โ Moles โ Ratio โ Molecular formula. Four letters, four steps. The order never changes.
- โ- Four-step algorithm: percentages โ moles โ smallest ratio โ multiply by n.
- โ- n = actual molar mass / empirical mass.
- โ- Always recompute the molar mass of your answer to catch arithmetic slips.
- โ- Empirical and molecular formulas are equal only when n = 1.
Array Address Calculation โ Flashcards (GATE CSE)
Cover the answer, recall, then check. 12 cards on array address arithmetic โ a recurring GATE numerical topic.
Q1. 1-D array: address of A[i] given base B, lower bound LB, element size w?
A1. Addr(A[i]) = B + (i โ LB) ร w. For 0-indexed C arrays (LB = 0): B + iยทw.
Q2. 2-D row-major address of A[i][j] for MรN array (M rows, N cols), 0-indexed, base B, size w?
A2. B + (iยทN + j)ยทw. Note it uses N (number of COLUMNS), not rows.
Q3. 2-D column-major address of A[i][j] for MรN array, 0-indexed?
A3. B + (jยทM + i)ยทw. Uses M (number of ROWS).
Q4. In row-major storage, which subscript varies fastest in memory?
A4. The last (rightmost / column) index โ a whole row sits contiguously.
Q5. In column-major storage, which subscript varies fastest?
A5. The first (leftmost / row) index โ a whole column sits contiguously.
Q6. Which order does C use? Which does Fortran / MATLAB use?
A6. C: row-major. Fortran, MATLAB, R: column-major.
Q7. Array declared A[L1..U1][L2..U2]: how many rows and columns?
A7. Rows = U1 โ L1 + 1, Cols = U2 โ L2 + 1.
Q8. int A[10][20], base 2000, 4 bytes/elt, row-major. Address of A[3][5]?
A8. 2000 + (3ร20 + 5)ร4 = 2000 + 65ร4 = 2000 + 260 = 2260.
Q9. Same array, column-major. Address of A[3][5]? (10 rows)
A9. 2000 + (5ร10 + 3)ร4 = 2000 + 53ร4 = 2000 + 212 = 2212.
Q10. 3-D row-major address of A[i][j][k], dimensions D1รD2รD3, 0-indexed?
A10. B + ((iยทD2 + j)ยทD3 + k)ยทw.
Q11. Row-major with lower bound 1 (A[1..M][1..N]): address of A[i][j]?
A11. B + ((i โ 1)ยทN + (j โ 1))ยทw.
Q12. Why can an array give O(1) random access?
A12. Because any element's address is computed directly by arithmetic from the index โ no traversal needed.
Array Address Calculation โ revision notes (GATE CSE)
Address-calculation questions are near-guaranteed easy marks in GATE Data Structures โ pure formula substitution once you know which convention (row-major vs column-major) and which index base (0 or 1) the question uses. The whole skill is careful bookkeeping.
Core formulas
For element size w, base address B:
1-D: Addr(A[i]) = B + (i โ LB)ยทw, where LB is the lower bound.
2-D, M rows ร N columns, 0-indexed:
- Row-major: B + (iยทN + j)ยทw โ uses N (columns)
- Column-major: B + (jยทM + i)ยทw โ uses M (rows)
2-D with lower bounds L1, L2 (A[L1..U1][L2..U2]):
- Row-major: B + ((i โ L1)ยทN + (j โ L2))ยทw, where N = U2 โ L2 + 1
- Column-major: B + ((j โ L2)ยทM + (i โ L1))ยทw, where M = U1 โ L1 + 1
3-D row-major (D1รD2รD3): B + ((iยทD2 + j)ยทD3 + k)ยทw.
Complexity table
| Operation | Cost | Reason |
|---|---|---|
| Random access A[i] | O(1) | Direct address arithmetic |
| Address computation | O(1) | Constant multiplications/additions |
| Sequential traversal | O(n) | Visit every element |
| Storage (n elements) | O(n) | Contiguous block |
Exam Tricks & Tips
- ๐ฏ Row-major uses columns, column-major uses rows. Memorise this one line and half the mistakes vanish.
- ๐ฏ Row-major = last index varies fastest (rows stored one after another); column-major = first index varies fastest.
- ๐ฏ Watch the index base. If the array starts at 1, subtract 1 from each subscript before applying the 0-indexed formula.
- ๐ฏ C is always row-major; Fortran / MATLAB / R are column-major. GATE loves testing this recall.
- ๐ฏ Number of elements before A[i][j] in row-major = iยทN + j (0-indexed) โ the offset in elements, multiply by w for bytes.
- โ Common mistake: using the number of rows M in the row-major formula (or columns in column-major). It is the other dimension.
Expected exam pattern
1-mark numerical: "A 2-D array A[10][20] with base X, element size w bytes, stored in row/column-major order โ find address of A[i][j]." Occasionally a 3-D variant, or a reverse question (given an address, find the index). Sometimes combined with lower-bound offsets to trap careless subtraction.
Quick recap
Addr = Base + offsetยทw. Offset for row-major 2-D = iยท(#cols) + j; for column-major = jยท(#rows) + i. Subtract lower bounds first if the array is not 0-indexed. C โ row-major, Fortran โ column-major. Arrays are O(1) access precisely because this offset is computable in constant time.
Array Address Calculation
When you write a[i], the compiler silently computes a memory address. For multi-dimensional arrays that arithmetic is exam material โ GATE regularly asks for the byte address of a given element, testing whether you know row-major from column-major order.
Core concept: an array occupies contiguous memory, so the address of any element is the base address plus an offset. The offset is (number of elements before it) ร (size of each element). The only subtlety is how a 2-D array is linearised: row-major (rows stored one after another) or column-major.
How it works
Beginner โ 1-D arrays
For A[i] with base address B, element size w, and lower bound L (often 0):
Address(A[i]) = B + (i โ L) ร w.
For a zero-indexed array, Address(A[i]) = B + i ร w.
Intermediate โ 2-D arrays
For an array A[m][n] (m rows, n columns), lower bounds 0, element size w:
- Row-major (C, most languages): Address(A[i][j]) = B + (i ร n + j) ร w.
- Column-major (Fortran, MATLAB): Address(A[i][j]) = B + (j ร m + i) ร w.
Row-major walks along a row first; column-major walks down a column first.
Advanced โ nonzero lower bounds and higher dimensions
With lower bounds L_r, L_c the row/column indices become (i โ L_r) and (j โ L_c). For a 3-D row-major array A[p][q][r]:
Address(A[i][j][k]) = B + ((i ร q + j) ร r + k) ร w.
The pattern generalises: multiply each index by the product of the sizes of all faster-varying dimensions.
Worked example
An array A[10][20] is stored row-major with base address 1000 and each element 4 bytes. Find the address of A[3][5] (indices from 0).
- Elements before it = i ร n + j = 3 ร 20 + 5 = 65.
- Address = 1000 + 65 ร 4 = 1000 + 260 = 1260.
Column-major would instead give 1000 + (5 ร 10 + 3) ร 4 = 1000 + 212 = 1212.
Exam relevance
GATE asks for the address of a specific element given base, size, dimensions, and the storage order โ sometimes with nonzero lower bounds. The row-major vs column-major distinction is the whole point; misreading it is the intended trap.
Tricks & shortcuts
- Row-major: the row index is multiplied by the number of columns (i ร n).
- Column-major: the column index is multiplied by the number of rows (j ร m).
- Always subtract lower bounds first if they are not zero.
Using the number of rows where the number of columns is needed (or vice versa). In row-major, the multiplier for the row index is the column count n, not the row count m. Swapping m and n is the single most common address-calculation error.
- โ- Address = base + (offset in elements) ร element size.
- โ- Row-major A[i][j]: base + (iยทn + j)ยทw.
- โ- Column-major A[i][j]: base + (jยทm + i)ยทw.
- โ- Subtract nonzero lower bounds from indices first.
- โArray indexing is address arithmetic: base plus offset times element size. For 2-D arrays the offset depends on storage order โ row-major multiplies the row index by the column count, column-major does the opposite. Watch the lower bounds and don't swap m and n.