Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

General: The purpose of this project is to get some experience with arrays, pointers and memory management. Mastery of these concepts is critical to C

General: 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.

Note: Before starting out, please read (and re-read) the instructions on writing and testing your project. [LIN K]

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 = Ai0 B0 j + Ai1 B 1 j + Ai2B 2 j + ... + 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.

STARTER CODE

#include  #include  #include "MatrixMultiply.h" void multiplyMatrices( double a[], const uint32_t a_rows, const uint32_t a_cols, double b[], const uint32_t b_cols, double c[]) { // https://en.wikipedia.org/wiki/Row-_and_column-major_order } double** multiplyMatricesPtr( double** a, const uint32_t a_rows, const uint32_t a_cols, double** b, const uint32_t b_cols) { return NULL; } // https://en.wikipedia.org/wiki/Transpose double** transposeMatrix( double** a, const uint32_t a_rows, const uint32_t a_cols) { return NULL; } 

Code is C, not C++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Analyse the basic sources of competitive advantage.

Answered: 1 week ago

Question

The two fluids that circular in the body?

Answered: 1 week ago

Question

Eliminated from the body?

Answered: 1 week ago

Question

What is accounting?

Answered: 1 week ago