Question: hI I was hoping you could help with this program, I have tried to write it but it has a lot of compile errors. ,

hI I was hoping you could help with this program, I have tried to write it but it has a lot of compile errors. , thank you.

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

 | a b | x | e f | = | a x e + b x g a x f + b x h| | c d | x | g h | = | c x e + d x g c x f + d x h| 

For example:

 | 3 1 | | 5 6 | |3*5+1*8 3*6+1*7 | |23 25 | 
| 4 2 |x| 8 7 | |4*5+2*8 4*6+2*7 | |36 38 | 

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

 | 1 0 | | 0 1 | 

"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

 det | a b | | c d | = ad - bc 

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:

 | a b | + | e f | = | a + e b + f | | c d | | g h | | c + g d + h | 

For example:

 | 3 1 | | 5 6 | | 3+5 1+6 | | 8 7 | | 4 2 |+ | 2 3 | = | 4+2 2+3 |= | 6 5 | 

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:

 2 x | 2 4 | = | 2*2 2*4 | = | 4 8 | | 3 1 | | 2*3 2*1 | | 6 2 | 

operator<<()

This function will be called when the stream insertion operator << is used to print a Matrix object. For example:

 int array1[2][2] = {{1, 9}, {2, 5}} // 2d array Matrix m1(array1); // A matrix created from that 2d array cout << m1; // Compiler generated function call: operator<<(cout, m1); 

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

 | 1 9 | | 2 5 | 

should produce the output

 [[1, 9], [2, 5]] 

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.

/********************************************************************* PROGRAM: CSCI 241 Assignment 4 PROGRAMMER: your name FUNCTION: This program tests the functionality of the Complex class. *********************************************************************/ #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 cout << "1. Testing identity matrix constructor "; const Matrix m1; cout << "m1 = " << m1 << endl << endl; // Test array initialization constructor cout << "2. Testing array initialization constructor "; Matrix m2(array1); cout << "m2 = " << m2 << endl; const Matrix m3(array2); cout << "m3 = " << m3 << endl << endl; // Test determinant cout << "3. Testing determinant "; cout << "det" << m2 << " = " << m2.determinant() << endl; cout << "det" << m3 << " = " << m3.determinant() << endl << endl; // Test matrix addition cout << "4. Testing matrix addition "; cout << m2 << " + " << m3 << " = " << m2 + m3 << endl; cout << m3 << " + " << m2 << " = " << m3 + m2 << endl << endl; // Test scalar multiplication cout << "5. Testing scalar multiplication "; cout << m2 << " * 2 = " << m2 * 2 << endl; cout << "4 * " << m2 << " = " << 4 * m2 << endl << endl; // Test matrix multiplication cout << "6. Testing matrix multiplication "; cout << m2 << " * " << m3 << " = " << m2 * m3 << endl; cout << m3 << " * " << m2 << " = " << m3 * m2 << endl; cout << m3 << " * " << m1 << " = " << m3 * m1 << endl; cout << m1 << " * " << m3 << " = " << m1 * m3 << endl << endl; cout << "det(m2 * m3) and det(m2) * det(m3) are "; cout << (((m2 * m3).determinant() == m2.determinant() * m3.determinant()) ? "equal " : "not equal "); // Test relational operators cout << "7. Testing relational operators "; const Matrix m4(array1); cout << m2 << " and " << m4; cout << ((m2 == m4) ? " are equal " : " are not equal "); cout << m2 << " and " << m3; cout << ((m2 != m3) ? " are not equal " : " are equal "); return 0; } 

Output

Output from a correctly functioning program will look like this:

1. Testing identity matrix constructor m1 = [[1, 0], [0, 1]] 2. Testing array initialization constructor m2 = [[5, 7], [3, 2]] m3 = [[2, 3], [1, 4]] 3. Testing determinant det[[5, 7], [3, 2]] = -11 det[[2, 3], [1, 4]] = 5 4. Testing matrix addition [[5, 7], [3, 2]] + [[2, 3], [1, 4]] = [[7, 10], [4, 6]] [[2, 3], [1, 4]] + [[5, 7], [3, 2]] = [[7, 10], [4, 6]] 5. Testing scalar multiplication [[5, 7], [3, 2]] * 2 = [[10, 14], [6, 4]] 4 * [[5, 7], [3, 2]] = [[20, 28], [12, 8]] 6. Testing matrix multiplication [[5, 7], [3, 2]] * [[2, 3], [1, 4]] = [[17, 43], [8, 17]] [[2, 3], [1, 4]] * [[5, 7], [3, 2]] = [[19, 20], [17, 15]] [[2, 3], [1, 4]] * [[1, 0], [0, 1]] = [[2, 3], [1, 4]] [[1, 0], [0, 1]] * [[2, 3], [1, 4]] = [[2, 3], [1, 4]] det(m2 * m3) and det(m2) * det(m3) are equal 7. Testing relational operators [[5, 7], [3, 2]] and [[5, 7], [3, 2]] are equal [[5, 7], [3, 2]] and [[2, 3], [1, 4]] are not equal 

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!