Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

implement a class called Matrix that will be used to represent a matrix of value

ID: 3535267 • Letter: I

Question

implement a class called Matrix that will be used to represent a matrix of values. This class will have the ability to add, subtract, and multiply two matrices.

The Matrix class definition should be placed at the top of a source code file while the method implementation should be done after the closing curly brace for main().

The Matrix class should contain five private data members. They are:

Note: For ease, the two-dimensional array of integers will always have a maximum of 5 rows, each of which has 5 columns. However, it's possible that the entire two-dimensional array will not be used. This is why the number of rows and columns is being kept as separate data members. So a 2x3 matrix, will have its data stored in the first 2 rows of the matrix and within the first 3 columns of those 2 rows.

The first constructor for the Matrix class (the default constructor) takes no arguments. Like all C++ constructors, it does not have a return data type.

It should set the number of rows and number of columns data members so that they hold the maximum values (ie. use the symbolic constants). The two-dimensional array should be set to all 0s.

The second constructor for the Matrix class should take two arguments: an integer that represents the desired number of rows for the matrix, and an integer that represents the desired number of columns for the matrix. DO NOT GIVE THESE ARGUMENTS THE SAME NAMES AS YOUR DATA MEMBERS. Like all C++ constructors, this constructor does not have a return data type.

If the desired number of rows is less than or equal to the maximum number of rows, use the desired number rows to initialize the number of rows data member. Otherwise, initialize the number of rows data member to the maximum number of rows.

If the desired number of columns is less than or equal to the maximum number of columns, use the desired number columns to initialize the number of columns data member. Otherwise, initialize the number of columns data member to the maximum number of columns.

The third constructor for the Matrix class should take three arguments: a two-dimensional array of integers (5x5), an integer that represents the number of rows in the passed in two-dimensional array of integers, and an integer that represents the number of columns in the passed in two-dimensional array of integers. DO NOT GIVE THESE ARGUMENTS THE SAME NAMES AS YOUR DATA MEMBERS. Like all C++ constructors, this constructor does not have a return data type.

If the number of rows in the passed in two-dimensional array of integers is less than or equal to the maximum number of rows, use it to initialize the number of rows data member. Otherwise, initialize the number of rows data member to the maximum number of rows.

If the number of columns in the passed in two-dimensional array of integers is less than or equal to the maximum number of columns, use it to initialize the number of columns data member. Otherwise, initialize the number of columns data member to the maximum number of columns.

The passed in two-dimensional array of integers should be used to initialize the two-dimensional array of integers data member.

This method is used to print/display a matrix. It takes no arguments and returns nothing.

Use a set of nested loops (probably for loops) to display the matrix in the form of a table.

This method adds two Matrix objects. It takes a Matrix object as its argument and returns a Matrix object.

Two matrices can only be added if their number of rows and number of columns are equal. If the matrices cannot be added together, display a meaningful error message and then execute return *this;.

These two matrices cannot be added together because they are different sizes.

If the matrices can be added together, it is done by adding together the integers that are located at corresponding spots of the matrices. So the values at [0][0] and at [0][0] of the passed in Matrix object (anotherMatrix) should be added together, the values at [0][1] and at [0][1] of anotherMatrix should be added together, etc... To do this:

Create a local Matrix object that has the same number of rows and columns as the two Matrix objects that are being added together.

Use a set of nested loops, to go through and add up the values that are in correspoding locations of the current instance and the passed in Matrix (anotherMatrix). The resulting sum should be placed in the corresponding location in the local Matrix object.

return the local Matrix object

This method subtracts a Matrix object from another Matrix object. It takes a Matrix object as its argument and returns a Matrix object.

Two matrices can only be subtracted if their number of rows and number of columns are equal. If the matrices cannot be subtracted, display a meaningful error message and then execute return *this;.

These two matrices cannot be subtracted because they are different sizes.

If the matrices can be subtracted, it is done in a manner similar to the addition from above where the integers that are located at corresponding spots of the matrices are used. So the value at [0][0] of the current instance has the value at [0][0] of the passed in Matrix object (anotherMatrix) subtracted from it, the value at [0][1] of the current instance has the value at [0][1] of anotherMatrix subtracted from it, etc... To do this:

Create a local Matrix object that has the same number of rows and columns as the two Matrix objects that are being subtracted.

Use a set of nested loops, to go through and subtract the values that are in correspoding locations of the current instance and the passed in Matrix (anotherMatrix). The resulting difference should be placed in the corresponding location in the local Matrix object.

return the local Matrix object

This method multiplies the current Matrix object by a scalar value. It takes an integer as its argument and returns a Matrix object.

Scalar multiplication is simple. Multiply each value in the current Matrix object by the passed in integer argument. To do this:

Create a local Matrix object that has the same number of rows and columns as the current Matrix object.

Use a set of nested loops, to go through the two dimensional array data member of the current instance. For each value in the array, multiply it by the passed in integer argument. The resulting product should be placed in the corresponding location in the local Matrix object.

return the local Matrix object

This method multiplies a Matrix object by another Matrix object. It takes a Matrix object as its argument and returns a Matrix object.

Two matrices can only be multiplied if the number of columns in the current Matrix object is equal to the number of rows in the passed in Matrix object. If the matrices cannot be multiplied, display a meaningful error message and then execute return *this;.

These two matrices cannot be multiplied because the number of columns in the current Matrix object (2) does not equal the number of rows in the passed in Matrix object (3).

If the matrices can be multiplied, it is done by calculating the dot product of the rows from the current instance and the columns of the passed in Matrix object. The dot product is calculated by taking each of the values in a row from the current instance and multiplying them by the corresponding values in the column of the passed in Matrix object. The resulting products are added together and the resulting sum is placed into a Matrix object. For example:

Repeat the process with the first row from the current instance and the second column from the the passed in Matrix object.

Finally, do the same thing using the second row of the current instance and the columns from the passed in Matrix object.

Notice that the resulting Matrix has a size equal to the number of rows in the current instance by the number of columns in the passed in Matrix object. Implenting this logic in C++ is a little difficult, so it's advisable to follow these steps:

Create a local Matrix object that has the same number of rows as the current instance and the same number of columns as the passed in Matrix object.

Create 6 integer subscripts. These will be used to access the 3 Matrix objects that are used in the calculation. For the logic that follows: currRowSuband currColSub will be used for the current instance; anotherRowSub and anotherColSub will be used for the passed in Matrix object; andlocalRowSub and localColSub will be used for the local Matrix object.

Create an integer variable to hold the dot product result

Implement the following:

The main() function has already been written. It can be downloaded from: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/pgm10.cpp.

Add the class definition at the top of the program and the methods at the bottom. Also don't forget to fill in the documentation box at the top and to document the methods for the class.

Each method must have a documentation box like a function.

Hand in a copy of your source code using Blackboard.

The output for the program should resemble:

Explanation / Answer

//working code please rate:

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

#include<process.h>

#define sk 100

class matrix

{

public:

int i,j,k,sum,r1,r2,c1,c2;

int m1[sk][sk],m2[sk][sk];

char reply;

void getin(void);

void mply(void);

void add(void);

void minus(void);

void read(void);

void input(void);

};

void matrix::getin()

{

cout<<" WELCOME TO SOLUTION OF TWO MATRICRES ";

cout<<" -------------------------------------";


cout<<"YOU CAN CALCULATE UPTO A LIMIT OF MATRIX 100 * 100 ";

cout<<"YOU CAN DO THE FOLLOWING :ADDITION SUBTRACTION";

cout<<"MULTIPLICATION AND READ ONLY ";

cout<<"DO YOU WANT TO CONTINUE [Y/N] : ";

cin >>reply;

if(reply=='y'||reply=='Y')

{

cout<<" Enter the number of rows and columns of matrix 1 : ";

cin>>r1>>c1;

cout<<"Enter the value of matrix 1 : "<<endl;

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

cin>>m1[i][j];

cout<<"Enter the number of rows and columns of matrix 2 : ";

cin>>r2>>c2;

cout<<"

Enter the value of matrix 2 : "<<endl;

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

cin>>m2[i][j];

}

else exit(0);

}

void matrix::mply()

{

cout<<"THE RESULT AFTER MULTIPLICATION IS : ";

if(c1==r2)

{

for(i=0;i<r1;i++)

{

cout<<endl;

for(j=0;j<c2;j++)

{

sum=0;

for(k=0;k<c1;k++)

sum+=m1[i][k]*m2[k][j];

cout<<setw(6)<<sum;

}

}

}

else

{

cout<<"

INVALID INPUT ";

cout<<"

MULTIPLICATION NOT POSSIBLE ";

}

}

void matrix::add()

{

cout<<"

THE RESULT AFTER ADDITION IS : ";

if(r1==r2&&c1==c2)

{

for(i=0;i<r1;i++)

{

cout<<endl;

for(j=0;j<c1;j++)

cout<<setw(3)<<(m1[i][j]+m2[i][j]);

}

}

else

{

cout<<"

INVALID INPUT ";

cout<<"

ADDITION NOT POSIIBLE ";

}

}

void matrix::minus()

{

cout<<"THE RESULT AFTER SUBTRACTION IS : ";

if(r1==r2&&c1==c2)

{

for(i=0;i<r1;i++)

{

cout<<endl;

for(j=0;j<c1;j++)

cout<<setw(3)<<(m1[i][j]-m2[i][j]);

}

}

else

{

cout<<"

INVALID INPUT ";

cout<<"

SUBTRACTION NOT POSSIBLE ";

}

}

void matrix::read()

{

cout<<"ENTERED MATRIX 1 IS : ";

for(i=0;i<r1;i++)

{

cout<<endl;

for(j=0;j<c1;j++)

cout<<setw(3)<<m1[i][j];

}

cout<<endl;

cout<<"ENTERED MATRIX 2 IS : ";

for(i=0;i<r2;i++)

{

cout<<endl;

for(j=0;j<c2;j++)

cout<<setw(3)<<m2[i][j];

}

}

void main()

{

int ans,option;

char response;

matrix m;

clrscr();

start :

m.getin();

again :

cout<<"

What you wish to do : "<<endl;

cout<<"1.ADDITION ";

cout<<"2.SUBTRACTION ";

cout<<"3.MULTIPLY ";

cout<<"4.READ ONLY ";

cout<<"5.EXIT ";

cout<<"ENTER YOUR CHOICE IN NUMBERS : ";

repeat :

cin>>ans;

switch(ans)

{

case 1 : m.add(); break;

case 2 : m.minus(); break;

case 3 : m.mply(); break;

case 4 : m.read(); break;

case 5 : goto exit ;

default :

{

cout<<"INVALID ENTRY ENTER YOUR CHOICE AGAIN : ";

goto repeat;

}

}

cout<<"DO YOU WANT TO DO IT FOR ANOTHER TIME [Y/N] : ";

cin>>response;

if(response=='y'||response=='Y')

{

wrong:

cout<<"Enter your choice in number ";

cout<<"1.FOR Same matrix 2.FOR Another matrix ";

cin>>option;

switch(option)

{

case 1 : goto again;

case 2 : goto start;

default: cout<<"

INVALID ENTRY "; goto wrong;

}

}

exit :

getch();

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote