Multiple choice

Consider an array of MxN order. Which of the following formulae gives the location of the element in Row-major ordering of elements?

  1. Loc(a[i][j]) = Base(a) + w[ (i-1) +N(j-1)]

  2. Loc(a[i][j]) = Base(a) + w[N(i-1) +(j-1)]

  3. Loc(a[i][j]) = Base(a) + w[M(i-1) +(j-1)]

  4. Loc(a[i][j]) = Base(a) + w[ M(i-1) +N(j-1)]

  5. Loc(a[i][j]) = Base(a) + w[ (i-1) +(j-1)]

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

This is the correct formula because it first computes the number of elements in the rows above the specified element, N(i - 1), and adds to it the number of elements in front of the specified element in the same row, (j - 1). w is word length. Base(a) is the address of first element.