Based on the given comments (SHOWN IN RED), complete thefollowing program. Some of the routines are implementedalready.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class TheMatrix;
// Function Prototype Section
TheMatrix operator/ (const TheMatrix& M1, constTheMatrix& M2);
TheMatrix operator- (const TheMatrix& M1, constTheMatrix& M2);
TheMatrix operator+ (const TheMatrix& M1, constTheMatrix& M2);
TheMatrix operator* (const TheMatrix& M1, constTheMatrix& 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 INTHE PROTOTYPE SECTION
// ABOVE TO BE FRIEND OF THIS CLASS
public:
int M[5][5]; // 5 by 5 matrix ofinteger
TheMatrix();
~TheMatrix();
};
//------------------------------------------
// Deconstructor
TheMatrix::~TheMatrix()
{
}
//------------------------------------------
//Default Constructor
TheMatrix::TheMatrix()
{
for(int i=0; i< 5; i++)
for (int j=0; j<5; j++)
M[j]=0;
}
//------------------------------------------
// This routine asks the user to enter a file name and grabthe
// 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[j];
return is;
}
//------------------------------------------
// Implement this routine to print the matrixto
// the output stream (in this case screen).
// Print in a form of 5 rows by 5columns
ostream& operator<< (ostream& os, constTheMatrix& myMatrix)
{
}
//------------------------------------------
// 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, constTheMatrix& M2)
{
TheMatrix Result;
for(int i=0; i< 5; i++)
for (int j=0; j<5; j++)
Result.M[j] = M1.M[j]+M2.M[j];
return(Result);
}
//------------------------------------------
// Implement the following routine to subtract twoMatrices
TheMatrix operator- (const TheMatrix& M1, constTheMatrix& M2)
{
}
//------------------------------------------
// Implement the following routine to multiply twomatrices
TheMatrix operator* (const TheMatrix& M1, constTheMatrix& M2)
{
}
//------------------------------------------
// Implement the following routine to divide twomatrices
// set the result value to zero if the division by zerooccurs
TheMatrix operator/ (const TheMatrix& M1, constTheMatrix& M2)
{
}
//------------------------------------------
int main()
{
TheMatrix A, B, AddAB, MulAB, SubAB, DivAB;
ifstream fin1, fin2;
fin1 >> A; // This code calls the functionoperator>> to grab information from a file and place it intoMatrix A
fin2 >> B; // This code calls the functionoperator>> to grab information from a file and place it intoMatrix B
// call a function to operator+ to add Matrix A and Btogether and place the result into Matrix AddAB
// call a function to operator- to subtract Matrix A fromMatrix B and place the result into Matrix SubAB
// call a function to operator* to multiply Matrix A and Btogether and place the result into Matrix MulAB
// call a function to operator+ to divide Matrix A by matrixB and place the result into Matrix DivAB
// call the function operator<< to print matrixA
// call the function operator<< to print matrixB
// call the function operator<< to print matrixAddAB
// call the function operator<< to print matrixSubAB
// call the function operator<< to print matrixMulAB
// call the function operator<< to print matrixDivAB
return 0;
)
You may place the following in the data file. For matrix1:
1 2 3
4 5 6
7 8 9
10 11 12
For matrix 2:
10 20 0
40 50 6
0 23 29
3 1 4
Even though, the above is 3 by 3 matrix, your programshould work for any n by n matrix.
Based on the given comments (SHOWN IN RED), complete the following program. Some of the routines are implemented already
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am