Question: You will need to write one class for this assignment. A main program to test your class will be provided. The matrix class The matrix

You will need to write one class for this assignment. A main program to test your class will be provided.

The matrix class

The matrix class represents a two by two square matrix using a two-dimensional array of integers. This class should be implemented as two separate files.

The class definition should be placed in a header file called matrix.h. Include header guards to make it impossible to accidentally #include it 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 member function descriptions below will refer to this as the matrix array.

In addition to the data member described above, the class definition should also contain prototypes for the member functions described below.

The implementations of the class member functions 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 member functions:

"Identity matrix" constructor

Parameters: This default constructor has no parameters.

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 a 2-by-2 matrix is given by

 

operator+()

Parameters: This member function 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 member function.

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:

 

operator*()

Parameters: This member function 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 member function.

Returns: The result of multiplying the elements of the matrix left operand by the integer right operand (a new matrix object).

Logic: The product Ac of a matrix A and a number c (also called a scalar in the parlance of abstract algebra) is computed by multiplying every entry of A by c: (Ac)i,j = Ai,j c. For example:

 

operator*()

Parameters: This member function 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 member function.

Returns: The result of multiplying the elements of the matrix left operand by the matrix right operand (a new matrix object).

Logic: The product of two 2-by-2 matrices is given by

 

For example:

 

Note that unlike ordinary multiplication, matrix multiplication is not commutative.

operator==()

Parameters: This member function 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 member function.

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 member function should return false.

operator!=()

Parameters: This member function 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 member function.

Returns: A Boolean value.

Logic: Return false if the left operand equals the right operand. Otherwise, the member function should return true.

Note: Member functions that do not alter the data members of the object that called them should be declared to be const. That will allow them to called using const objects.

In addition to the member functions described above, you will need to write two standalone functions. These functions are not (and can not be) member functions. 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:

 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 m1 (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

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

operator*()

Parameters: This member function 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).

-----------------------------------------------------------------------------

main.cpp:

#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;

cout << m3 << " * 2 = " << m3 * 2 << endl;

cout << "4 * " << m3 << " = " << 4 * m3 << 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 ");

cout << m3 << " and " << m4;

cout << ((m3 == m4) ? " are equal " : " are not equal ");

cout << m1 << " and " << m3;

cout << ((m1 != m3) ? " are not equal " : " are equal ");

return 0;

}

----------------------------------------------------------------------

makefile:

# Compiler variables

CXX = g++

CXXFLAGS = -Wall -Werror -ansi -pedantic -std=c++14

# Rule to link object code files to create executable file

assign6: assign6.o matrix.o

$(CXX) $(CCFLAGS) -o assign6 $^

# Rules to compile source code files to object code

assign6.o: assign6.cpp matrix.h

matrix.o: matrix.cpp matrix.h

# Pseudo-target to remove object code and executable files

clean:

rm -f assign6 *.o

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!