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

  • 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);...

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

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

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • 47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all...

    47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all the rows of column n of the matrix A. b. Refers to the elements in all the rows between columns m and n of the matrix A. c. Refers to the elements in all the columns between rows m and n of the matrix A. d. Refers to the elements in all the columns of row n of the matrix A. 48. What does...

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

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

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