I need help with this homework. Need Program design and the C++ code. Pr ogr amm
ID: 3885929 • Letter: I
Question
I need help with this homework. Need Program design and the C++ code.
Pr ogr amming Assignment: Abstract Data Type - Matrix The purpose of this course is 1. identification of the appropriate ABSTRACT DATA TYPE (model and 2. selection of the best DATA STRUCTURE and corresponding AlGORITHMS to provide you with experience in: operations) to use for a given application to implement the ADT in a programming lan plications in mathematics, economics, engineering, There are many applications in mathematics. ecco etc. in which the underlying model is a matrix . Consequently, it woul implement this ADT. The special case of a 2 x 2 matrix will be considered MODEL: t general form of a 2 x 2 matrix is: a11 a12 2.5 3-7 example: a21 a22 4 a11, a12, a21, and a22 are real numbers (scalars) that are the CoMPONENTS (or ELEMENTS) of the matrix. where: OPERATIONS: the following are some of the common operations that are performed on such matrices: matrix addition: Ia11 a12 ! a21 a22 ! a11 b a12 b12 a21 b2a22 b22 b11 b12 b21 b22 matrix subtraction: a11 -b a12 b12! a21-b a22b22 " a11 a12 : b1 b12 ! a21 a22 b21 b22 scalar multiplication: a1 a 12 k x a11 x a12' a21 a22 k x a21 k x a22Explanation / Answer
Given below is the code for the question. The operations could be defined by operator overloading in the matrix class. But since I am not sure if the operator overloading is covered for you in class, implemented the code in the functions described in the question. The output is generated in a file named "output.txt". You can check the output in the file. Hope the answer helped. If it did, please don't forget to rate it . Thank you very much.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class MATRIX
{
private:
double elements[2][2]; //the elements of a 2x2 matrix
public:
MATRIX()
{
//initialize all to 0
for(int i = 0; i < 2; i ++)
for(int j = 0; j < 2; j++)
elements[i][j] = 0;
}
//return the element in specified row and col
double getElement(int row, int col)
{
return elements[row][col];
}
//set teh element in the specified row and col to the specified value
void setElement(int row, int col, double value)
{
elements[row][col] = value;
}
};
void get_matrix(string &name, MATRIX &mat);
//sum is passed by reference so the changes are preserved, the sum of m1 and m2 is stored in sum
void calc_sum(MATRIX m1, MATRIX m2, MATRIX &sum);
//diff is passed by reference so the changes are preserved, the difference of m1 and m2 is stored in diff
void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff);
void get_scalar(double &b);
//k_m is passed by reference so the changes are preserved, the result of scalar multiplication is stored in k_m
void scalar_mult(double k, MATRIX m, MATRIX &k_m);
//prod is passed by reference so the changes are preserved, the result of matrix product is stored in prod
void calc_prod(MATRIX m1, MATRIX m2, MATRIX &prod);
//m_inv is passed by reference so the changes are preserved, hte inverse of matrix is stored in m_inv
void calc_inv(MATRIX m, MATRIX &m_inv);
void print_matrix(ofstream &out, string name, MATRIX m);
int main()
{
string name1, name2;
MATRIX m1, m2;
double scalar;
MATRIX sum, diff, prod, scal_prod, inv;
get_matrix(name1, m1);
get_matrix(name2, m2);
get_scalar(scalar);
calc_sum(m1, m2, sum);
calc_diff(m1, m2, diff);
calc_prod(m1, m2, prod);
calc_inv(m1, inv);
scalar_mult(scalar, m1, scal_prod);
string filename = "output.txt";
ofstream outfile(filename.c_str());
if(!outfile.is_open())
{
cout << "Error opening output file " << filename << endl;
return 1;
}
print_matrix(outfile, name1, m1);
print_matrix(outfile, name2, m2);
print_matrix(outfile, "Sum = m1 + m2 ", sum);
print_matrix(outfile, "Difference = m1 - m2 ", diff);
print_matrix(outfile, "Product = m1 * m2", prod);
print_matrix(outfile, "Inverse of m1", inv);
print_matrix(outfile, "Scalar product of m1 and " + to_string(scalar), scal_prod);
outfile.close();
cout << "output written to file " << filename << endl;
}
void get_matrix(string &name, MATRIX &mat)
{
double value;
cout << "Enter the name of the matrix: " ;
cin >> name;
cout << "Enter the elements row-wise for Matrix " << name << endl;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
cin >> value;
mat.setElement(i, j, value);
}
}
//sum is passed by reference so the changes are preserved, the sum of m1 and m2 is stored in sum
void calc_sum(MATRIX m1, MATRIX m2, MATRIX &sum)
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
double value = m1.getElement(i, j) + m2.getElement(i, j);
sum.setElement(i, j, value);
}
}
//diff is passed by reference so the changes are preserved, the difference of m1 and m2 is stored in diff
void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff)
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
double value = m1.getElement(i, j) - m2.getElement(i, j);
diff.setElement(i, j, value);
}
}
void get_scalar(double &b)
{
cout << "Enter a scalar value: ";
cin >> b;
}
//k_m is passed by reference so the changes are preserved, the result of scalar multiplication is stored in k_m
void scalar_mult(double k, MATRIX m, MATRIX &k_m)
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
double value = m.getElement(i, j) * k;
k_m.setElement(i, j, value);
}
}
//prod is passed by reference so the changes are preserved, the result of matrix product is stored in prod
void calc_prod(MATRIX m1, MATRIX m2, MATRIX &prod)
{
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
double value = 0;
for(int k = 0; k < 2; k++)
{
value = value + m1.getElement(i, k) * m2.getElement(k, j);
}
prod.setElement(i, j, value);
}
}
}
//m_inv is passed by reference so the changes are preserved, hte inverse of matrix is stored in m_inv
void calc_inv(MATRIX m, MATRIX &m_inv)
{
double det = m.getElement(0, 0) * m.getElement(1, 1) - m.getElement(1, 0) * m.getElement(0, 1);
//set diagnoal elemnts top left and bottom right
m_inv.setElement(0, 0, m.getElement(1, 1) / det);
m_inv.setElement(1, 1, m.getElement(0, 0) / det);
//set diagnoal elemnts top right and bottom left
m_inv.setElement(0, 1, -m.getElement(0, 1) / det);
m_inv.setElement(1, 0, -m.getElement(1, 0) / det);
}
//prints the given matrix in the output stream
void print_matrix(ofstream &out, string name, MATRIX m)
{
out << fixed << setprecision(2); //show decimal point and 2 places after point
out << name << endl;
out << "====================" << endl;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
out << setw(10) << m.getElement(i, j);
out << endl;
}
out << endl;
}
output
Enter the name of the matrix: m1
Enter the elements row-wise for Matrix m1
7.0 2.5
-3.0 2.0
Enter the name of the matrix: m2
Enter the elements row-wise for Matrix m2
8.0 6.5
5.0 1.3
Enter a scalar value: 4.5
output written to file output.txt
output file : output.txt
m1
====================
7.00 2.50
-3.00 2.00
m2
====================
8.00 6.50
5.00 1.30
Sum = m1 + m2
====================
15.00 9.00
2.00 3.30
Difference = m1 - m2
====================
-1.00 -4.00
-8.00 0.70
Product = m1 * m2
====================
68.50 48.75
-14.00 -16.90
Inverse of m1
====================
0.09 -0.12
0.14 0.33
Scalar product of m1 and 4.500000
====================
31.50 11.25
-13.50 9.00