Hi, I cannot seem to get this assignment, my code is not compiling. Thank you. I
ID: 3813708 • Letter: H
Question
Hi, I cannot seem to get this assignment, my code is not compiling. Thank you.
In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The dimensions of the matrix below are 2 x 2 (read "two by two"), because there are two rows and two columns.
The individual items in a matrix are called its elements or entries. Provided that they are the same size (have the same number of rows and columns), two matrices can be added or subtracted element by element. The rule for matrix multiplication, however, is that two matrices can be multiplied only when the number of columns in the first equals the number of rows in the second. Any matrix can be multiplied element-wise by a scalar from its associated field.
If the matrix is square (has the same number of rows as columns), it is possible to deduce some of its properties by computing its determinant. For example, a square matrix has an inverse if and only if its determinant is not zero.
Note that inn mathematics, the rows and columns of a matrix are numbered starting from 1, not 0 like a C++ array.
Program
You will need to write one class for this assignment. A main program to test your class has been provided.
The Matrix class
The Matrix class represents a two by two square matrix using a two-dimensional array of integers. Like the other classes we've written this semester, this class should be implemented as two separate files.
The class declaration should be placed in a header file called Matrix.h. Include header guards to prevent it from accidentally being #included more than once in the same source code file.
The Matrix class should contain the following private data member:
a two-dimensional array of integers with two rows and two columns. The method descriptions below will refer to this as the matrix array.
In addition to the data member described above, the class declaration should also contain prototypes for the methods described below.
The implementations of the class methods should be placed in a separate source code file called Matrix.cpp. Make sure to #include "Matrix.h" at the top of this file.
The Matrix class should have the following methods:
Logic: See the logic for the other scalar multiplication operator function described below. This method's logic is the same; only the order of the operands is different.
This method does not alter any data members of the Matrix object that called the method, so it should be declared const.
Logic: The product of two 2-by-2 matrices is given by
For example:
Note that unlike ordinary multiplication, matrix multiplication is not commutative.
This method does not alter any data members of the Matrix object that called the method, so it should be declared const.
"Identity matrix" constructor
Parameters: This default constructor takes no arguments.
Logic: Set the elements of the matrix array to the "identity matrix", such that all the elements on the main diagonal are equal to 1 and all other elements are equal to 0, e.g.:
"Array initialization" constructor
Parameters: This constructor takes one argument, a two-dimensional array of integers with two rows and two columns.
Logic: Set the elements of the matrix array to the corresponding elements in the array passed into the constructor.
determinant()
Parameters: None.
Returns: The integer determinant of the Matrix object.
Logic: The determinant of 2-by-2 matrices is given by
operator+()
Parameters: This method takes one parameter, a reference to a constant Matrix object, representing the right operand of the matrix addition expression. The left operand of the expression is represented by this, which points to the Matrix object that called the method.
Returns: The result of the matrix addition of the left and right operands (a new Matrix object).
Logic: The sum A+B of two 2-by-2 matrices A and B is calculated entrywise: (A + B)i,j = Ai,j + Bi,j, where 1 i 2 and 1 j 2:
For example:
This method does not alter any data members of the Matrix object that called the method, so it should be declared const.
operator*()
Parameters: This method takes one parameter, an integer representing the right operand of the scalar multiplication. The left operand of the expression is represented by this, which points to the Matrix object that called the method.
Returns: The result of multiplying the elements of the matrix left operand by the integer right operand (a new Matrix object).
operator*()
Parameters: This method takes one parameter, a reference to a constant Matrix object, representing the right operand of the matrix multiplication expression. The left operand of the expression is represented by this, which points to the Matrix object that called the method.
Returns: The result of multiplying the elements of the matrix left operand by the matrix right operand (a new Matrix object).
operator==()
Parameters: This method takes one parameter, a reference to a constant Matrix object, representing the right operand of the relational expression. The left operand of the expression is represented by this, which points to the Matrix object that called the method.
Returns: A boolean value.
Logic: Return true if all elements of the left operand are equal to the corresponding elements of the right operand. Otherwise, the method should return false.
This method does not alter any data members, so it should be declared const.
operator!=()
Parameters: This method takes one parameter, a reference to a constant Matrix object, representing the right operand of the relational expression. The left operand of the expression is represented by this, which points to the Matrix object that called the method.
Returns: A boolean value.
Logic: Return false if the left operand equals the right operand. Otherwise, the method should return true.
This method does not alter any data members, so it should be declared const.
In addition to the methods described above, you will need to write two standalone functions. These functions are not (and can not be) methods. You should
Include a friend declaration for each of these functions in the Matrix class declaration.
Put the definitions for these functions in Matrix.cpp.
Logic: The product cA of a number c (also called a scalar in the parlance of abstract algebra) and a matrix A is computed by multiplying every entry of A by c: (cA)i,j = c · Ai,j. For example:
operator<<()
This function will be called when the stream insertion operator << is used to print a Matrix object. For example:
In the example code above, the ostream object cout (the left operand in the stream insertion expression) will be passed to the function as the first parameter, while the Matrix object c (the right operand) will be passed to the function as the second parameter.
Parameters: This function takes two parameters. The first is a reference to an ostream object, representing the left operand of the stream insertion expression. The second is a reference to a constant Matrix object, representing the right operand of the expression.
Returns: A reference to an ostream object (i.e., the first parameter).
Logic: Print the elements of the matrix separated by commas. Use square brackets around each row of the matrix and around the matrix as a whole.
For example, printing the object m1 from the example above, which represents the 2x2 matrix
should produce the output
operator*()
Parameters: This method takes two parameters, an integer representing the left operand of the scalar multiplication, and a reference to a constant Matrix object, representing the right operand of the scalar multiplication.
Returns: The result of multiplying the elements of the matrix right operand by the integer left operand (a new Matrix object).
Driver Program
A short main program to test your class is given below. Either type in (or copy and paste) this program or copy it to your UNIX account from the pathname ~t90kjm1/CS241/Code/Spring2016/Assign4/assign4.cpp.
Output
Output from a correctly functioning program will look like this:
Other Points
A makefile is required. Use the makefile for Assignment 2 as your model.
The driver program is designed to make this assignment easy to develop incrementally. Simply comment out all of the lines of the driver program that call methods or functions you haven't written yet. You should be able to write, test, and debug one method or function at a time:
"Identity matrix" constructor
operator<<() function
"Array initialization" constructor
determinant() method
operator+() method (matrix addition)
operator*() method (scalar multiplication with integer as right operand)
operator*() function (scalar multiplication with integer as left operand)
operator*() method (matrix multiplication)
operator==() method
operator!=() method
95 12Explanation / Answer
#include #include "Matrix.h" using std::cout; using std::endl; int main() { int array1[2][2] = {{5, 7}, {3, 2}}; int array2[2][2] = {{2, 3}, {1, 4}}; // Test identity matrix constructor coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.