The purpose of this project is to get some experience with arrays, pointers and memory management. Mastery of these concepts is critical to C programming. Unless you really know what you’re doing with pointers and memory allocation, you are a danger to society (well, figuratively speaking, we hope). When I wrote this assignment, I presumed that you understood the mathematical construct of a Matrix, and the calculation necessary to multiply two matrices together.
Your Mission: Edit the file “Project2.cpp” and implement the functions multiplyMatrices, transposeMatrix, and multiplyMatricesPtr.
Stage 1, Matrix Multiplication: Recall the mathematical definition of a matrix product. Given an MxN matrix A (M rows and N columns), and an NxK matrix B, calculate the MxK result matrix C as follows:
Each element Cij = Ai0B0j + Ai1B1j + Ai2B2j + … + Ai(n-1)B(n-1)j
Every element of C must be computed this way. So, we’ll need two nested while loops, one for i (which goes from 0 to M, the number of rows in A), and one loop for j, (which goes from 0 to K the number of columns in B). Nested at the innermost level will be yet another while loop (I call mine the “k-loop”) which goes from 0 to N and calculates the sum for each Cij.
Your function should have these three loops, one nested inside the other. You must, however, explicitly code the function to use row-major ordering for the matrix storage. That means that Aij is stored in the location a[i * a_cols + j] where a_cols is the variable holding the number of columns in A (N in the discussion above). The matrices B and C are similarly stored in the arrays b[] and c[] respectively. For your convenience, the code you are given for multiplyMatrices defines the variables a_rows, a_cols, b_rows, b_cols, c_rows and c_cols (well, some are parameters, others are defined as local variables, some may not be there). You may not need to use all these variables. If you decide not to use them, please delete the variable definitions.
Stage 2, Matrix Multiplication with Dynamic Matrices: Implement function multiplyMatricesPtr that works with dynamic matrices. Each dynamic matrix consists of an array of pointers such that each element in the array points to one row of the matrix. (See class materials for details.) Both the array of points, as well as each row, are dynamically allocated. As the result of multiplyMatricesPtr function, you should return a new dynamic matrix (of appropriate size) that contains the result of multiplication. We will “free” your matrix, so if you do not allocate appropriate size (or if you change the format of the matrix), the program will crash.
Stage 3, Matrix Transpose: Implement function transposeMatrixPtr. Same as in the previous stage, the resulting matrix should be dynamically allocated. The format of the matrix is described in the previous stage. We will “free” your matrix, so if you do not allocate in the format that we expect the program will crash.
Testing
You may develop your code any way you like, but the final testing should be on kamek (not luigi, or any of the other 64-bit servers), using our Makefile.