Contents    Page-10    Prev    Next    Page+10    Index   

If an array reference x[i][j] occurs inside nested for loops,

  • A: i should be the inner loop in most languages
  • B: j should be the inner loop in most languages
  • C: doesn't matter
  • D: only matters with specially optimized CPUs
  • E: does not depend on language

    Answer: B

    All modern languages (except FORTRAN) store arrays in row-major order, which gives best memory locality (smallest address jump between memory references) when j is the inner loop.

    Rule: The rightmost subscript should be the inner loop.