Starting from:

$25

CSCI1730 - Project 1 - Matrix Class - Solved

Problem / Exercise
Your goal for this project is to create an easy-to-use Matrix class in C++ that makes use of dynamic memory allocation and includes basic matrix operations both in the form of regular functions. Certain aspects of this project might be taught concurrently with the project. Other aspects of this project may not be covered in lecture. You are encouraged to use the DEITEL text and cppreference.com   as references. Also, please ask questions via Piazza. Specifically, the following chapters from the textbook may be useful for this project:

•    DEITEL 3.1–37 (pp. 73–102)

•    DEITEL 8.1–812 (pp. 339–384)

•    DEITEL 9.1–9.16 (pp. 385–433)

•    DEITEL 10.9 (pp. 466–468)

Here is the basic prototype for the Matrix class (you may need to add more to it to support some of the additional features listed further down in this document):

typedef unsigned int uint; class Matrix { public:

Matrix(uint rows, uint cols);
// constructor (all elements initialized to 0)
Matrix(const Matrix & m);
// copy constructor
~Matrix();
// destructor
Matrix add(double s) const;
// add scalar to this matrix
Matrix add(const Matrix & m) const;
// add this matrix and another matrix
Matrix subtract(double s) const;
// subtract scalar from this matrix
Matrix subtract(const Matrix & m) const;
// subtract another matrix from this matrix
Matrix multiply(double s) const;
// multiply this matrix by a scaler
Matrix multiply(const Matrix & m) const;
// multiply this matrix by another matrix
Matrix divide(double s) const;
// divide this matrix by a scalar
Matrix t() const;
// transpose of this matrix
const uint numRows() const;
// returns the number of rows
const uint numCols() const;
// returns the number of cols
double & at(uint row, uint col);
// get/set element at row,col
const double & at (uint row, uint col) const; // get element at row,col (when using a const object)

}; // Matrix

Important Class Details
Your Matrix implementation will contain elements of type double. In the prototype presented above, the term scalar refers to a regular number. For example, if you add a scalar to a matrix, then each element in the matrix gets that number added to it. Contrast this with the member functions that take a Matrix as their parameter. These functions represent regular matrix operations. For some of these operations (e.g., multiplication, transpose, etc.), you may need to consult some sort of reference in order to recall/learn the exact procedure/meaning behind the operation.

NOTE: You MAY assume valid input for all operations.

NOTE: You MAY NOT use library classes such as std::array, std::vector, std::list, etc. for this project. You must implement your Matrix class internally using a dynamically allocated array.

Example Usage 1
Matrix a(2, 2);

a.at(0, 0) = 1; // [ 1, 2 ]

a.at(0, 1) = 2; // [ 1, 3 ]

a.at(1, 0) = 1;

a.at(1, 1) = 3;

Matrix b(2, 1);

b.at(0, 0) = 3; // [ 3 ]

b.at(1, 0) = 2; // [ 2 ]

Matrix c = a.multiply(b);

cout << "[ " << c.at(0, 0) << " ]" << endl // [ 7 ] << "[ " << c.at(1, 0) << " ]" << endl; // [ 9 ]

The usage of the member function at(uint, uint) is what facilities our ability to perform operations such as a.at(0, 0) = 1. If you implement this function carefully, then this behavior should work because the function returns a reference to an element.

1          C++ Code & Program
1.1     Setup
Make sure that all of your files are in a directory called LastName-FirstName-p1, where LastName and FirstName are replaced with your actual last and first names, respectively.

1.2       Source Code Files
You should organize your project into the following files:

•    Matrix.h: This file should include the class prototype presented above. You MAY NOT modify the function prototypes that are included in the Matrix class prototype. However, you may add additional function prototypes and variables to the class prototype as needed. Make sure that this header file also contains an include guard (i.e., the #ifndef macro, etc.).

•    Matrix.cpp: This file should contain the implementation of your class’s functions.

•    p1.cpp: This file should contain a small/moderately sized driver that demonstrates the full range of functionality of your Matrix class.

Additionally, make sure that you adhere to the following:

•    All functions must be documented using Javadoc-style comments. Use inline documentation, as needed, to explain ambiguous or tricky parts of your code.

1.3 Makefile File
You need to include a makefile. Your makefile needs to compile and link separately. Make sure that your Matrix.cpp file compiles to Matrix.o. This is very important because we will be testing your submission by linking against your Matrix.o file. The resulting executable should be called p1. The first command in your makefile must perform the linking step which generates the executable. Please also define a “run” command that executes your program and a “clean” command that removes all the object files and the executable.

Make sure that when you compile, you pass the following options to g++:

-Wall -pedantic-errors

1.4 README File
Make sure to include a README file that includes the following information presented in a reasonably formatted way:

•    Your Name and UGA ID (811#)

•    Instructions on how to compile and run your program.

NOTE: Try to make sure that each line in your README file does not exceed 80 characters. Do not assume line-wrapping. Please manually insert a line break if a line exceeds 80 characters.

1.5      Compiler Warnings
Since you should be compiling with both the -Wall and pedantic-errors options, your code is expected to compile without g++ issuing any warnings. For this project, compiling without warnings will be one or more of the test cases.

1.6      Memory Leaks
Since this project makes use of dynamic memory allocation, you are expected to ensure that your Matrix implementation doesn’t result in any memory leaks. We will test for memory leaks using the valgrind utility. For this project, having no memory leaks will be one or more of the test cases.

More products