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

I need help with this homework. I need the Design and also the c++ code and can

ID: 3885763 • Letter: I

Question

I need help with this homework. I need the Design and also the c++ code and can you please explain what you did. thank you

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 a22

Explanation / Answer

//matrix.cpp

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class MATRIX

{

private:

string name;

int size;

double mat[2][2];

double scalerValue;

public:

MATRIX()

{

}

MATRIX(double m[2][2])

{

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

this->mat[i][j] = m[i][j];

}

}

}

void get_matrix(string name, MATRIX m)

{

this->name = name;

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

this->mat[i][j] = m.mat[i][j];

}

}

}

void get_scalar(double b)

{

this->scalerValue = b;

}

void calc_sum(MATRIX m1, MATRIX m2, MATRIX sum)

{

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

sum.mat[i][j] = m1.mat[i][j] + m2.mat[i][j];

}

}

}

void calc_diff(MATRIX m1, MATRIX m2, MATRIX diff)

{

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

diff.mat[i][j] = m1.mat[i][j] - m2.mat[i][j];

}

}

}

void scalar_mult(double k, MATRIX m , MATRIX k_m)

{

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

k_m.mat[i][j] = (m.mat[i][j])*k;

}

}

}

void calc_prod(MATRIX m1, MATRIX m2, MATRIX prod)

{

int i, j, k;

for (i = 0; i < 2; i++)

{

for (j = 0; j < 2; j++)

{

prod.mat[i][j] = 0;

for (k = 0; k < 2; k++)

prod.mat[i][j] += m1.mat[i][k] * m2.mat[k][j];

}

}

}

void print_matrix(ofstream o,string name, MATRIX m)

{

o << name << " = ";

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 2; j++)

{

o << m.mat[i][j] << " ";

}

o << endl;

}

o << endl;

}

};

int main()

{

double mat1[2][2] = { {7.0,2.5}, {-3.0,2.0} };

double mat2[2][2] = { {8.0,6.5},{5.0,1.3} };

MATRIX m1(mat1);

MATRIX m2(mat2);

m1.get_matrix("m1", mat1);

m2.get_matrix("m2", mat2);

double k;

cout << "Enter scalar value:";

cin >> k; //4.5

m1.get_scalar(k);

m2.get_scalar(k);

MATRIX sum;

sum.calc_sum(m1, m2, sum);

MATRIX diff;

diff.calc_diff(m1, m2, diff);

MATRIX scalar;

scalar.scalar_mult(k, m1, scalar);

MATRIX mult;

mult.calc_prod(m1, m2, mult);

}