public Matrix<T, A> add(Matrix<T, A> right) throws MatrixException { Matrix<T, A
ID: 3538386 • Letter: P
Question
public Matrix<T, A> add(Matrix<T, A> right) throws MatrixException { Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);if (rowSize != right.rowSize || columnSize != right.columnSize) throw new MatrixException("Cannot add matrices of different sizes");
setup(temp, rowSize, columnSize);
for (int i = 0; i < rowSize; ++i) { for (int j = 0; j < columnSize; ++j) temp.matrix[i][j] = arithmetics.add(matrix[i][j], right.matrix[i][j]); }
return temp; }
// TODO Implement subtract and multiply methods similar to add method
public Matrix<T, A> subtract(Matrix<T, A> right) throws MatrixException { Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);
if (rowSize != right.rowSize || columnSize != right.columnSize) throw new MatrixException("Cannot subtract matrices of different sizes");
setup(temp, rowSize, columnSize);
for (int i = 0; i < rowSize; ++i) { for (int j = 0; j < columnSize; ++j) temp.matrix[i][j] = arithmetics.subtract(matrix[i][j], right.matrix[i][j]); } return temp; }
public Matrix<T, A> add(Matrix<T, A> right) throws MatrixException { Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);
if (rowSize != right.rowSize || columnSize != right.columnSize) throw new MatrixException("Cannot add matrices of different sizes");
setup(temp, rowSize, columnSize);
for (int i = 0; i < rowSize; ++i) { for (int j = 0; j < columnSize; ++j) temp.matrix[i][j] = arithmetics.add(matrix[i][j], right.matrix[i][j]); }
return temp; }
// TODO Implement subtract and multiply methods similar to add method
public Matrix<T, A> subtract(Matrix<T, A> right) throws MatrixException { Matrix<T, A> temp = new Matrix<T, A>(arithmetics, rowSize, columnSize);
if (rowSize != right.rowSize || columnSize != right.columnSize) throw new MatrixException("Cannot subtract matrices of different sizes");
setup(temp, rowSize, columnSize);
for (int i = 0; i < rowSize; ++i) { for (int j = 0; j < columnSize; ++j) temp.matrix[i][j] = arithmetics.subtract(matrix[i][j], right.matrix[i][j]); } return temp; }
Explanation / Answer
#include <iostream>
int main()
{
int arr1[20][20],arr2[10][10];
int result[20][20];
int m,n;
int i,j;
// prompts the user to enter the rows and columns of the matrix
std::cout<<" Enter the number of rows (m) : ";
std::cin>>m;
std::cout<<" Enter the number of columns (n) : ";
std::cin>>n;
std::cout<<" Enter the elements of the 1st matrix ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" Element "<<i<<" : "<<j<<" --> ";
std::cin>>arr1[i][j];
}
}
std::cout<<" Enter the elements of the 2nd matrix : ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" Element "<<i<<" : "<<j<<" --> ";
std::cin>>arr2[i][j];
}
}
//------------ display matrics -----------------------
std::cout<<" 1st matrix ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" "<<arr1[i][j];
}
std::cout<<" ";
}
std::cout<<" 2nd matrix ";
for (i=0; i<m; i++)
{
for (j=0; j<n;j++)
{
std::cout<<" "<<arr2[i][j];
}
std::cout<<" ";
}
std::cout<<" Resultatn matrix ";
int k=0;
// multiplies the two matrices together
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
result[i][j] = 0;
for(k=0;k< m;k++)
{
result[i][j] = result[i][j] + arr1[i][k] * arr2[k][j];
}
} // end of j sub loop
} // end of i main loop
// displays the resultant matrix
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<result[i][j]<<" ";
}
std::cout<<" ";
}
return 0;
} // end of main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.