Question & Answer: Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which ad…..

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

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: 1 5 0 1 3 2 Enter the elements of matrix 2 row by row: 1 1 1 0 2 4 matrix 1 == matrix 2? No matrix 1 + matrix 2: 2 6 1 1 5 6 matrix 1power n. Enter n: 2 1 25 0 1 9 4

Expert Answer

 

/****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

Question & Answer: Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which ad..... 1

 

Still stressed from student homework?
Get quality assistance from academic writers!