Question

Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add which adds two matrices; power which raises the first matrix to power n; · which returns true if both matrices are equal. You need to overload the C++ equality operator A sample run follows. Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of matrix 1 row by row: 103 5 1 2 Enter the elements of matrix 2 row by row 1 1 2 10 4 matrix 2? matrix 1 No matrix 1 matrix 2: 2 1 5 6 1 6 matrix 1 power n. Enter n: 2 10 9 25 1 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/****Tested on online c++ compiler on linux system ***/

#include <iostream>
#include <cmath>
using namespace std;
/**class matrix**/
class matrix
{
/** private variable declaration **/
private:long m[5][5];
int row;int col;
/** public function declaration**/
public:
void getdata();
int operator ==(matrix);
matrix add(matrix);
matrix power(int);
void printMatrix(matrix);
};


/* function to check whether the matrix1 and matrix2 are same or not */
int matrix::operator==(matrix cm)
{
int flag =1;
if(row==cm.row && col==cm.col) {
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(!m[i][j]==cm.m[i][j]){
flag=0;
}
}
}
if(flag==1){
return 1;
}
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata() {
cout<<"Enter the number of rows: ";
cin>>row;
cin.clear();
cout<<"Enter the number of columns: ";
cin>>col;
cin.clear();
cout<<"enter the elements of the matrix row by row"<<endl;
  
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
cin>>m[i][j];
}
cout<<endl;
}
cin.clear();
}
/* function to add two matrix */
matrix matrix::add(matrix am) {
matrix temp;
if(row==am.row && col==am.col) {
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
} else{
cout<<"can not add. order of the input matrices is not identical";
return temp;
}
}
/* function to power of two matrix */
matrix matrix::power(int n) {
matrix temp;
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
temp.m[i][j]=pow(m[i][j],n);
}
temp.row=row;
temp.col=col;
}
return temp;
}
/** function to print matrix**/
void matrix::printMatrix(matrix am){
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
cout<< am.m[i][j]<<" ";
}
cout<<endl;
}
}

/* main function */


int main() {
matrix m1,m2,m3,m4;
int n;
// calling getdata function
m1.getdata();
m2.getdata();
// equality check
cout<<"matrix 1 == matrix 2?"<<endl;
if(m1==m2){
cout<<"Yes"<<endl;
} else{
cout<<"No"<<endl;
}
// sum of matrix
m3=m1.add(m2);
cout<<"matrix1 + matrix2:"<<endl;
// print matrix
m3.printMatrix(m3);
cout<<"matrix1 power n. ";
cout<<"Enter n:";
cin>>n;
cin.clear();
// calling power function
m4 = m1.power(n);
m4.printMatrix(m4);
return 0;
}

/**************************output************************/

sh-4.2$ g++ -o main *.cpp                                                                                                                                                       

sh-4.2$ main                                                                                                                                                                    

Enter the number of rows: 2                                                                                                                                                     

Enter the number of columns: 3                                                                                                                                                  

enter the elements of the matrix row by row                                                                                                                                     

1 0 3                                                                                                                                                                           

                                                                                                                                                                                

5 1 2                                                                                                                                                                           

                                                                                                                                                                                

Enter the number of rows: 2                                                                                                                                                     

Enter the number of columns: 3                                                                                                                                                  

enter the elements of the matrix row by row                                                                                                                                     

1 1 2                                                                                                                                                                           

                                                                                                                                                                                

1 0 4                                                                                                                                                                           

                                                                                                                                                                                

matrix 1 == matrix 2?                                                                                                                                                           

No                                                                                                                                                                              

matrix1 + matrix2:                                                                                                                                                              

2 1 5                                                                                                                                                                           

6 1 6                                                                                                                                                                           

matrix1 power n. Enter n:2                                                                                                                                                      

1 0 9                                                                                                                                                                           

25 1 4                 

codingground COMPILE AND EXECUTE C ONLINE * Project ▼ File ▼ G Edit ▼ d. View ▼ d> Shut Down > Help *New Project-20170812囧다+«                                                                                                                                                         

Thanks a lot

Add a comment
Know the answer?
Add Answer to:
Implement a C++ class to model the mathematical operations of a matrix. Your class should include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...

    Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods described: 1. read: reads in a matrix from the command line 2. display: prints the matrix to the command line 3. getRows: returns the number of rows 4. getCols: returns the number of columns 5. set: sets the double value at a particular column/row position 6. get: returns the value stored at a particular column/row position 7. Plus: returns a new Matrix object that...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT