In C++ A sample run follows. Enter the elements of matrix 1 row by row: 103 512
ID: 3860726 • Letter: I
Question
In C++
A sample run follows.
Enter the elements of matrix 1 row by row: 103
512
Enter the elements of matrix 2 row by row: 112
104
matrix 1 + matrix 2: 215
616
matrix 1 power n. Enter n: 2 109
25 1 4
This is my program
***//TestMatrix.pp//***
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
int i, j;
int** a, b;
int row, col;
MatrixMath p1, p2;
cout << " Specify number of rows of Matrix: ";
cin >> row;
setRow(row)
cout << " Specify number of columns of Matrix: ";
cin >> col;
a = (int**) malloc(row*sizeof(int*));
for (i=0; i<row; i++)
p1.a[i] = (int*) malloc(col*sizeof(int));
b = (int**) malloc(row*sizeof(int*));
for (i=0; i<row; i++)
b[i] = (int*) malloc(col*sizeof(int));
cout << " Enter elements of matrix 1 row by row: ";
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
cin >> a[i][j];
}
}
cout << " Enter elements of matrix 2 row by row: ";
//Reading matrix 2
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
cin >> b[i][j];
}
}
cout << " matrix 1 == matrix 2? ";
if(a == b)
cout << " Yes ";
else
cout << " No ";
return 0;
}
***// Matrix.h//***
# include <iostream>
# include <iomanip>
# include <cmath>
#include <cstdlib>
using namespace std;
class MatrixMath
{
/
private:
int** A;
int** B;
int rows, cols;
public:
MatrixMath();
MatrixMath(int** a, int** b, int row, int col);
void setMA(int** a);
void setMB(int** b);
void add();
void power();
bool operator==(MatrixMath &othr)const;
};
***// TestMatrix.cpp//***
# include "Matrix.h"
using namespace std;
//Constructor
MatrixMath::MatrixMath()
{
A = 0;
B = 0;
rows = 0;
cols = 0;
}
MatrixMath::MatrixMath(int** a, int** b, int row, int col)
{
A = a;
B = b;
rows = row;
cols = col;
}
void MatrixMath::setMA(int** a)
{
A = a;
}
void MatrixMath::setMB(int** b)
{
B =b;
}
void MatrixMath::add()
{
int i,j;
cout << " Matrix 1 + Matrix 2: ";
for(i=0; i<rows; i++)
{
/
for(j=0; j<cols; j++)
{
cout << left << setw(5) << (A[i][j] + B[i][j]);
}
cout << " ";
}
}
void MatrixMath::power()
{
int i,j, n;
cout << " Matrix 1 power n. Enter n: ";
cin >> n;
cout << " ";
for(i=0; i<rows; i++)
{
/
for(j=0; j<cols; j++)
{
cout << left << setw(5) << pow((double)A[i][j], (double)n);
}
cout << " ";
}
}
bool MatrixMath::operator==(MatrixMath &othr)const
{
int i, j;
/
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
/
if(A[i][j] != B[i][j])
{
return false;
}
}
}
return true;
}
Can anyone help me with that please
Explanation / Answer
Executable code:
File: Matrix.h
#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
//Class declaration
class MatrixMath
{
//Instance variables
private:
//Matrices to hold data
int** A;
int** B;
int rows, cols;
//Public methods
public:
//Constructor
MatrixMath();
//Addition operation
void add();
//Power of a matrix
void power();
//Overloading == operator
bool operator==(MatrixMath &othr)const;
//Destructor
~MatrixMath();
};
#endif // MATRIX_H_INCLUDED
File: Matrix.cpp
# include <iostream>
# include <iomanip>
# include <math.h>
# include "Matrix.h"
using namespace std;
//Constructor
MatrixMath::MatrixMath()
{
int i, j;
//Accept rows of matrix
cout << " Specify number of rows of Matrix: ";
cin >> rows;
//Accept columns of matrix
cout << " Specify number of columns of Matrix: ";
cin >> cols;
//First create a 1D array
A = (int**) malloc(rows*sizeof(int*));
//Next create memory for each element
for (i=0; i<rows; i++)
A[i] = (int*) malloc(cols*sizeof(int));
//First create a 1D array
B = (int**) malloc(rows*sizeof(int*));
//Next create memory for each element
for (i=0; i<rows; i++)
B[i] = (int*) malloc(cols*sizeof(int));
cout << " Enter elements of matrix 1 row by row: ";
//Reading matrix 1
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cin >> A[i][j];
}
}
cout << " Enter elements of matrix 2 row by row: ";
//Reading matrix 2
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cin >> B[i][j];
}
}
cout << " matrix 1 == matrix 2? ";
//Comparing matrices
if(A == B)
cout << " Yes ";
else
cout << " No ";
}
//Adds two matrices
void MatrixMath::add()
{
int i,j;
cout << " Matrix 1 + Matrix 2: ";
//Loop over rows
for(i=0; i<rows; i++)
{
//Loop over columns
for(j=0; j<cols; j++)
{
//Print element
cout << left << setw(5) << (A[i][j] + B[i][j]);
}
cout << " ";
}
}
//Power of a Matrix
void MatrixMath::power()
{
int i,j, n;
//Reading n value
cout << " Matrix 1 power n. Enter n: ";
cin >> n;
cout << " ";
//Loop over rows
for(i=0; i<rows; i++)
{
//Loop over columns
for(j=0; j<cols; j++)
{
//Print element
cout << left << setw(5) << pow((double)A[i][j], (double)n);
}
cout << " ";
}
}
//Overloading == operator
bool MatrixMath::operator==(MatrixMath &othr)const
{
int i, j;
//Loop over rows
for(i=0; i<rows; i++)
{
//Loop over columns
for(j=0; j<cols; j++)
{
//Comparing values
if(A[i][j] != B[i][j])
{
return false;
}
}
}
return true;
}
MatrixMath::~MatrixMath()
{
int i, j;
//Free up the memory allocated
for (i = 0; i < rows; i++)
{
free(A[i]);
}
free(A);
for (i = 0; i < rows; i++)
{
free(B[i]);
}
free(B);
}
File: main.cpp
#include <iostream>
#include "Matrix.h"
using namespace std;
//Main function
int main()
{
//Creating object
MatrixMath obj;
//Adding matrices
obj.add();
//Calculating power
obj.power();
cout << " ";
return 0;
}