CS312 Lab 5:  Column Major and Row Major

 

This is a very small lab to make sure you understand the difference between matrix representations in column major and row major. Given the following definitions, where A,B and C,D represent the same pair of two matrices in row major and column major, respectively.

#define M 2
#define L 2
#define N 3

int main() {
  //row major
  int A[M*L] = {1, 3, 2, 4};
  int B[L*N] = {5, 7, 9, 6, 8, 10};

  //column major
  int C[M*L] = {1, 2, 3, 4};
  int D[L*N] = {5, 6, 7, 8, 9, 10};


  int AB[M*N];
  int CD[M*N];
}

Implement the matrix multiplication to compute the results of A*B in row major multiplication and C*D in column major multiplication. Note that you should end up with the same matrix, i.e. matrix with the same values, just in corresponding row or column-major order, respectively. One should be the transpose of the other.

Note that the terms row-major and column-major is also used in programming languages to describe the way elements of multidimensional arrays are laid out in memory. Remember that memory is always one-dimensional. In this context, row major means elements are stored by rows, one row after another. This is the method used by C/C++. Fortran, Matlab, OpenGL and others use column-major.