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

Dropbox link if somethings doesnt show: problem: https://www.dropbox.com/s/vf0yq

ID: 3546948 • Letter: D

Question

Dropbox link if somethings doesnt show:

problem:

https://www.dropbox.com/s/vf0yq3vfd0zo5yn/ch8.png

Fig 8.9:

https://www.dropbox.com/s/8rn4lmk2motogll/fig08_09.cpp



Revise class Matrix from Fig 8.9 by adding member function power. This member function should take integer input parameter n and a Matrix output parameter mToTheN. If the default object is a square matrix (same number of rows as columns), power should store in mToTheN the default matrix raised to the nth power. Raising a square matrix M to a power n is defined as the product of n copies of M: M 0 = I (I is the identity matrix -- 1 s on the diagonal. 0s elsewhere) M n = M M for n>l

Explanation / Answer

// Program that multiplies one matrix by another; matrices' maximum
// sizes are MAX_ROWS x MAX_COLS
//
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_ROWS = 6;
const int MAX_COLS = 6;
class Matrix { // Represents a varying-size matrix that // can be input from a file
public:
Matrix power(int );
Matrix() {}
Matrix( int, int, int ); // Constructor that initializes matrix
// size and sets all valid elements to
// given initial value
Matrix& operator*=( const Matrix& );
private:
int rows;
int cols;
int mat[MAX_ROWS][MAX_COLS];
friend ostream& operator<< ( ostream&, const Matrix& );
friend istream& operator>> ( istream&, Matrix& );
};
//
// Inputs two matrices from files designated by the user.
// Forms and displays their product.
//
int main()
{
Matrix m1;
Matrix m2;
string file1Name;
string file2Name;
cout << "Name of file containing first matrix => ";
cin >> file1Name;
ifstream infilOne( file1Name.c_str(), ios::in );
infilOne >> m1;
infilOne.close();
/*
cout << "Name of file containing second matrix => ";
cin >> file2Name;
ifstream infilTwo( file2Name.c_str(), ios::in );
infilTwo >> m2;
infilTwo.close();
if (!infilOne.fail() && ! infilTwo.fail()) {
cout << endl << "Matrix 1" << endl << endl << m1 << endl << endl;
cout << "Matrix 2" << endl << endl << m2 << endl << endl;
m1 *= m2;
cout << "Matrix 1 x Matrix 2 = " << endl << endl << m1 <<
endl << endl;
} else {
cout << "Error in input" << endl;
}*/
cout << endl << "Matrix 1" << endl << endl << m1 << endl << endl;
cout << endl << "Matrix to the power 4 is " << endl << endl << m1.power(4) << endl << endl;
return 0;
}
//
// Constructor that initializes a Matrix object of size initRows
// x initCols, setting matrix elements to initValue
//
Matrix :: Matrix( int initRows, int initCols, int initValue )
{
rows = initRows;
cols = initCols;
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
mat[i][j] = initValue;
}
//
// Multiplies m1 by m2 if these matrices are conformable.
// Otherwise, displays an error message and sets m1's size
// to 0 x 0
//
Matrix& Matrix :: operator*=(const Matrix& m2 ) // input
{
int val, i, j, k;
Matrix prod;
if (cols != m2.rows) {
cout << "Matrices are not conformable." << endl;
prod.rows = 0;
prod.cols = 0;
} else {
prod.rows = rows;
prod.cols = m2.cols;
for (i = 0; i < prod.rows; ++i)
for (j = 0; j < prod.cols; ++j) {
val = 0;
for (k = 0; k < cols; ++k)
val += mat[i][k] * m2.mat[k][j];
prod.mat[i][j] = val;
}
}
*this = prod;
return *this;
}
//
// Writes to the output stream the contents of matrix m,
// one row at a time
//
ostream& operator<< ( ostream& os, const Matrix& m )
{
for (int i = 0; i < m.rows; ++i) {
for (int j = 0; j < m.cols; ++j)
cout << setw( 5 ) << m.mat[i][j];
cout << endl;
}
return os;
}
//
// Gets the number of rows and columns from the input
// stream. If rows <= MAX_ROWS and cols <= MAX_COLS,
// fills m with data from file; otherwise signals failure
// on stream is. In the event of input failure, sets m size
// to 0 x 0
//
istream& operator>> ( istream& is, Matrix& m )
{
int i, j;
is >> m.rows >> m.cols;
if (is.fail()) {
m.rows = 0;
m.cols = 0;
} else if (m.rows > MAX_ROWS || m.cols > MAX_COLS) {
is.setstate( ios::failbit);
m.rows = 0;
m.cols = 0;
} else {
for (i = 0; i < m.rows; ++i)
for (j = 0; j < m.cols; ++j)
is >> m.mat[i][j];
if (is.fail()) {
m.rows = 0;
m.cols = 0;
}
}
return is;
}

Matrix Matrix::power(int k)
{
Matrix copy_matrix(*this);
if(rows!=cols)
{
cout << "Matrix is not square matrix so power not possible. returning same matrix. " << endl;
return copy_matrix;
}
while(--k)
copy_matrix*= (*this);
return copy_matrix;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote