**C++ Procedural Code** Write a program,assignment11.cpp, that reads two matrice
ID: 3684096 • Letter: #
Question
**C++ Procedural Code** Write a program,assignment11.cpp, that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and3) multiplies them (if compatible) and prints their product. Prompt the user for thenames of two matrix input files (see format below) and place the output in a file ofthe user’s choosing.The format of the input files should contain the number of rows, followed by thenumber of columns, followed by the matrix data; here is an example of a 2 by 3 matrix input file:
2 3
1 -1 0
2 0 1
Although the examples here use integers as elements of matrices, your codeshould allow matrices to contain doubles.
Here is an example of the output expected:
Matrix A = 1 -1 0 2 1 1 0 -1 1
Matrix B = 1 -2 1 0 1 0 1 -1 1
A + B = 2 -3 1 2 2 1 1 -2 2
A - B = 0 1 -1 2 0 1 -1 0 0
A * B = 1 -3 1 3 -4 3 1 -2 1
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class TheMatrix;
// Function Prototype Section
TheMatrix operator- (const TheMatrix& M1, const TheMatrix& M2);
TheMatrix operator+ (const TheMatrix& M1, const TheMatrix& M2);
TheMatrix operator* (const TheMatrix& M1, const TheMatrix& M2);
ifstream& operator>> (ifstream& is, TheMatrix& myMatrix);
ostream& operator<< (ostream& is, const TheMatrix& myMatrix);
// End of prototype section
class TheMatrix
{
// MAKE ALL THE FUNCTIONS DEFINED IN THE PROTOTYPE SECTION
// ABOVE TO BE FRIEND OF THIS CLASS
friend TheMatrix operator- (const TheMatrix& M1, const TheMatrix& M2);
friend TheMatrix operator+ (const TheMatrix& M1, const TheMatrix& M2);
friend TheMatrix operator* (const TheMatrix& M1, const TheMatrix& M2);
friend ifstream& operator>> (ifstream& is, TheMatrix& myMatrix);
friend ostream& operator<< (ostream& is, const TheMatrix& myMatrix);
public:
int M[5][5]; // 5 by 5 matrix of integer
TheMatrix();
~TheMatrix();
};
//------------------------------------------
// Destructor
TheMatrix::~TheMatrix()
{
}
//------------------------------------------
//Default Constructor
TheMatrix::TheMatrix()
{
for (int i = 0; i< 5; i++)
for (int j = 0; j<5; j++)
M[i][j] = 0;
}
//------------------------------------------
// This routine asks the user to enter a file name and grab the
// data from there to place it into a matrix
// the matrix filled up with the data is returned
ifstream& operator>> (ifstream& is, TheMatrix& myMatrix)
{
string fileName;
cout << "Enter a file name -> ";
cin >> fileName;
is.open(fileName.data());
for (int i = 0; i< 5; i++)
for (int j = 0; j<5; j++)
is >> myMatrix.M[i][j];
return is;
}
//------------------------------------------
// Implement this routine to print the matrix to
// the output stream (in this case screen).
// Print in a form of 5 rows by 5 columns
ostream& operator<< (ostream& os, const TheMatrix& myMatrix)
{
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
os << right << setw(5) << myMatrix.M[i][j];
}
os << endl;
}
os << endl;
}
//------------------------------------------
// This routine adds two matrices and creating a new matrix
// of the addition of the two matrices M1 and M2
TheMatrix operator+ (const TheMatrix& M1, const TheMatrix& M2)
{
TheMatrix Result;
for (int i = 0; i< 5; i++)
for (int j = 0; j<5; j++)
Result.M[i][j] = M1.M[i][j] + M2.M[i][j];
return(Result);
}
//------------------------------------------
// Implement the following routine to subtract two Matrices
TheMatrix operator- (const TheMatrix& M1, const TheMatrix& M2)
{
TheMatrix Result;
for (int i = 0; i< 5; i++)
for (int j = 0; j<5; j++)
Result.M[i][j] = M1.M[i][j] - M2.M[i][j];
return(Result);
}
//------------------------------------------
// Implement the following routine to multiply two matrices
TheMatrix operator* (const TheMatrix& M1, const TheMatrix& M2)
{
TheMatrix Result;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
for(int k = 0; k < 5; k++){
Result.M[i][j] += M1.M[i][k] * M2.M[k][j];
}
}
}
return(Result);
}
//------------------------------------------
int main()
{
TheMatrix A, B, AddAB, MulAB, SubAB, DivAB;
ifstream fin1, fin2;
ofstream fout1, fout2;
fin1 >> A; // This code calls the function operator>> to grab information from a file and place it into Matrix A
fin2 >> B; // This code calls the function operator>> to grab information from a file and place it into Matrix B
// call a function to operator+ to add Matrix A and B together and place the result into Matrix AddAB
AddAB = A+B;
// call a function to operator- to subtract Matrix A from Matrix B and place the result into Matrix SubAB
SubAB = A-B;
// call a function to operator* to multiply Matrix A and B together and place the result into Matrix MulAB
MulAB = A*B;
// call a function to operator+ to divide Matrix A by matrix B and place the result into Matrix DivAB
//DivAB = A/B;
// call the function operator<< to print matrix A
cout << A;
// call the function operator<< to print matrix B
cout << B;
// call the function operator<< to print matrix AddAB
cout << AddAB;
// call the function operator<< to print matrix SubAB
cout << SubAB;
// call the function operator<< to print matrix MulAB
cout << MulAB;
return 0;
}
dat1.txt
1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 5 6 4 7 3
dat2.txt
3 5 2 6 7 0 1 6 7 3 2 4 6 1 0 3 8 9 2 5 7 1 8 4 9
sample output
Enter a file name -> dat1.txt
Enter a file name -> dat2.txt
1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
4 3 2 1 0
5 6 4 7 3
3 5 2 6 7
0 1 6 7 3
2 4 6 1 0
3 8 9 2 5
7 1 8 4 9
4 7 5 10 12
6 8 14 16 3
11 12 13 7 5
7 11 11 3 5
12 7 12 11 12
-2 -3 1 -2 -2
6 6 2 2 -3
7 4 1 5 5
1 -5 -7 -1 -5
-2 5 -4 3 -6
56 56 108 51 78
61 141 183 111 108
94 134 202 149 162
19 39 47 49 42
65 106 157 102 115
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.