C++ HELP PLEASE In this project you will be defining a Template Matrix class in
ID: 3909090 • Letter: C
Question
C++ HELP PLEASE
In this project you will be defining a Template Matrix class in C++ and equip the class with a few matrix operations. If the code does not compile – no credit is given. The code must compile, and the main function execute successfully. The Matrix class must have the following private data members at least:
1. A variable to represent rows for the number of rows of the matrix
2. A variable column for the number of columns of the matrix
3. A pointer variable data to point to the matrix data. The Matrix class must also have the following member functions. 1. A default constructor – this constructor initializes the row and column of the matrix to 3, then dynamically allocates memory for the elements of the matrix. The constructor also initializes the Matrix elements to random integers between 0 and 10. 2. A constructor with parameters - The parameters to the constructor are used to initialize the row and column variables. This constructor dynamically allocates memory for the elements of the matrix. The constructor also initializes the Matrix elements to random integers between 0 and 10. 3. A correct destructor.
4. A method called display() that displays the matrix in proper row/column form.
5. The method getElement(int i, int j) that will return the (i,j) element in the matrix. Include exception handling with object out_of_range for the case when i or j or both are out of bounds
6. The method setElement ( int i, int j, int value) that set the (i,j) element of the matrix to the value. Include exception handling with object out_of_range for the case when i or j or both are out of bounds.
7. The method for the addition of one matric object to another add(Matrix a) that receives a Matrix object a and adds it to the implicit object and returns another Matrix object. Include exceptions if matrices cannot be added ( e.g. in the case of incompatible types or dimensions)
8. The method multiply multiply( Matrix a) that receives a Matrix object and multiplies it to the implicit object and returns another Matrix object. Include exceptions if matrices cannot be multiplies (incompatible dimensions). An algorithm for matrix multiplication is given on page 205 of your textbook.
9. Overload the out stream operator “<<” for the Matrix class to display a Matrix Object
10. Overload the in stream operator “>>” for the Matrix class to receive elements for a Matrix object.
11. Make proper use of const and references.
Write a main(…) method to text the Matrix class. The main(…) method must include:
1. Declare two 3x3 matrices.
2. Test setElement() and getElement() methods
3. Test the exceptions by using invalid parameters
4. Use the operator “<<”to print both matrices.
5. Use the operator “>>” change the values of one of the matrices. Print the new matrix.
6. Call the add method and display the result.
7. Call the multiply method and display the result.
Here is what was provided for the main_func.cpp and matrix.h files. I need them completed per above instructions.
#include "matrix.h"
#include <ctime>
#include <string>
size_t sizemat = 6;
// debug flag
unsigned trace_me = 0;
int main(int argc, char* argv[])
{
using std::string;
if (argc > 1)
{
if (string(argv[1]) == "traceon")
trace_me = 1;
}
// set random seed
srand(time(NULL));
matrix<double> m1(sizemat, sizemat);
matrix<double> m2(sizemat, sizemat);
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), (double)(rand() % 10));
}
}
m1.display();
cout << " ===== calling getElement " << endl;
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
cout << m1.getElement((h + 1), (k + 1)) << " ";
}
cout << endl;
}
cout << endl << endl << m1 << endl;
// TESTING ADD FUNCTION
// how would we call add
matrix<double> result(sizemat, sizemat);
// fill m1 and m2 with data
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), 1);
m2.setElement((h + 1), (k + 1), 1);
}
}
// display m1 and m2
cout << "BEFORE ADDING m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
// add the matrices
result = m1.add(m2);
cout << "AFTER ADDING ---- m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
cout << "result of adding m1 and m2 is: ";
result.display();
// TESTING MULTIPLY FUNCTION
matrix<double> mult_result(sizemat, sizemat);
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), 1);
m2.setElement((h + 1), (k + 1), 1);
}
}
// display m1 and m2
cout << "BEFORE MULTIPLY m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
// multiply matrices
mult_result = m1.multiply(m2);
cout << "AFTER MULTIPLY ---- m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
cout << "result of multiplying m1 and m2 is: ";
mult_result.display();
return 0;
}
matrix.h
#pragma once
#include <iostream>
#include <ctime>
using std::cout;
using std::endl;
template< typename T >
class matrix;
template< typename T1>
std::ostream& operator<<(std::ostream& o, const matrix<T1>& t);
template< typename T >
class matrix
{
public:
matrix()
{
}
matrix(const size_t& r, const size_t& c)
:m_rows(r), m_cols(c)
{
m_pdata = new T*[r];
for (int i = 0; i < r; i++)
m_pdata[i] = new T[c];
}
matrix(const matrix<T> & that)
{
if (this == &that)
{
// flag error/exception -
// TO DO for class
// throw exception
}
// start to clone the other class
this->m_rows = that.m_rows;
this->m_cols = that.m_cols;
// allocate our own buffer
this->m_pdata = new T*[m_rows];
for (int i = 0; i < m_rows; i++)
this->m_pdata[i] = new T[m_cols];
// now copy content of other 'that' matrix
if (that.m_pdata != nullptr)
{
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
m_pdata[h][k] = that.m_pdata[h][k];
}
}
}
}
matrix<T>& operator=(const matrix<T> that)
{
if (this == &that)
{
// Class TO DO raise exception
}
if (this->m_rows != that.m_rows ||
this->m_cols != that.m_cols)
{
// throw exception - matrix size mismatch
}
// now copy content of other 'that' matrix
if (that.m_pdata != nullptr)
{
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
m_pdata[h][k] = that.m_pdata[h][k];
}
}
}
else
{
// for class to think about
}
return *this;
}
~matrix()
{
if (m_pdata != nullptr)
{
for (int i = 0; i < m_rows; i++)
{
if (m_pdata[i] != nullptr)
{
delete[] m_pdata[i];
m_pdata[i] = nullptr;
}
}
}
delete[] m_pdata;
m_pdata = nullptr;
}
void setElement(const size_t& i, const size_t& j, const T value)
{
if (i == 0 || i > m_rows
|| j == 0 || j > m_cols)
{
// throw C++ error exception
}
else
m_pdata[i - 1][j - 1] = value;
}
T getElement(const size_t& i, const size_t& j)
{
if (i == 0 || i > m_rows
|| j == 0 || j > m_cols)
{
// throw C++ error exception
}
else
return m_pdata[i - 1][j - 1];
}
void display()
{
cout << " ";
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
cout << m_pdata[h][k] << " ";
}
cout << endl;
}
cout << " ";
}
matrix<T> add(const matrix<T>& that)
{
matrix<T> answer(that.m_rows, that.m_cols);
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
// the resulting cumulative sum matrix element is
answer.m_pdata[h][k] =
// this matrix'es element
this->m_pdata[h][k]
// plus
+
// the other 'that' matrix'es element
that.m_pdata[h][k];
}
}
if (trace_me)
{
cout << "inside matrix<T>::add() function - answer is";
answer.display();
}
return answer;
}
matrix<T> multiply(const matrix<T> & that)
{
if (this->m_cols != that.m_rows)
{
// throw exception - matrix col-row incompatible
}
// Output matrix C pg 205
// in Algorithm 4.3.19
matrix<T> answer(this->m_rows, that.m_cols);
// This the individual row of the answer matrix C
// that we iterate over
for (size_t row = 0; row < m_rows; row++)
{
for ( size_t thiscol = 0; thiscol < this->m_cols; thiscol++ )
{
// debug tracing to help
if (trace_me)
{
cout << "inside matrix<T>::multiply() function ";
answer.display();
}
// this is the cumulative multiplied sum
// Cij in Algorithm 4.3.19 page 205
// C(i,j) = 0
answer.m_pdata[row][thiscol] = 0;
for ( size_t thatsrow = 0; thatsrow < that.m_rows; thatsrow++ )
{
// cumulative inner loop
// multiplied sum for Cij
// Algorithm 4.3.19 page 205
// C(i,j) = C(i,j) + A(i,k) * B(k,j)
answer.m_pdata[row][thiscol] +=
this->m_pdata[row][thiscol] * that.m_pdata[thatsrow][thiscol];
}
}
}
if (trace_me)
{
cout << "Final resul to of matrix<T>::multiply() ";
answer.display();
}
// finally return the answer
return answer;
}
template< typename T>
friend std::ostream& operator<<(std::ostream& o, const matrix<T>& t);
template< typename T1>
friend std::istream& operator >> (std::istream& o, const matrix<T1>& t);
private:
// TO DO for us
// matrix<T> operator=(const matrix<T>&);
size_t m_rows = 0;
size_t m_cols = 0;
T** m_pdata = nullptr;
};
template< typename T1>
std::ostream& operator<<(std::ostream& o, const matrix<T1>& t)
{
for (size_t h = 0; h < t.m_rows; h++)
{
for (size_t k = 0; k < t.m_cols; k++)
{
o << t.m_pdata[h][k] << " ";
}
o << endl;
}
return o;
}
template< typename T1>
std::istream& operator >> (std::istream& o, const matrix<T1>& t)
{
// please place you code in basket
// place item in basket
return o;
}
Explanation / Answer
Matrix.h
#pragma once
#include <iostream>
#include <ctime>
using std::cout;
using std::endl;
using std::out_of_range;
using std::exception;
template< typename T >
class matrix;
template< typename T1>
std::ostream& operator<<(std::ostream& o, const matrix<T1>& t);
template< typename T >
class matrix
{
public:
matrix()
{
}
matrix(const size_t& r, const size_t& c)
:m_rows(r), m_cols(c)
{
m_pdata = new T*[r];
for (int i = 0; i < r; i++)
m_pdata[i] = new T[c];
}
matrix(const matrix<T> & that)
{
if (this == &that)
{
// flag error/exception -
// TO DO for class
// throw exception
throw exception("Trying to assign the same address.");
}
// start to clone the other class
this->m_rows = that.m_rows;
this->m_cols = that.m_cols;
// allocate our own buffer
this->m_pdata = new T*[m_rows];
for (int i = 0; i < m_rows; i++)
this->m_pdata[i] = new T[m_cols];
// now copy content of other 'that' matrix
if (that.m_pdata != nullptr)
{
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
m_pdata[h][k] = that.m_pdata[h][k];
}
}
}
}
matrix<T>& operator=(const matrix<T> that)
{
if (this == &that)
{
// Class TO DO raise exception
throw exception("Trying to assign the same address.");
}
if (this->m_rows != that.m_rows ||
this->m_cols != that.m_cols)
{
// throw exception - matrix size mismatch
throw exception("matrix size mismatch");
}
// now copy content of other 'that' matrix
if (that.m_pdata != nullptr)
{
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
m_pdata[h][k] = that.m_pdata[h][k];
}
}
}
else
{
// for class to think about
m_pdata = NULL;
}
return *this;
}
~matrix()
{
if (m_pdata != nullptr)
{
for (int i = 0; i < m_rows; i++)
{
if (m_pdata[i] != nullptr)
{
delete[] m_pdata[i];
m_pdata[i] = nullptr;
}
}
}
delete[] m_pdata;
m_pdata = nullptr;
}
void setElement(const size_t& i, const size_t& j, const T value)
{
if (i == 0 || i > m_rows
|| j == 0 || j > m_cols)
{
// throw C++ error exception
throw out_of_range("Passed i and j values are out of range.");
}
else
m_pdata[i - 1][j - 1] = value;
}
T getElement(const size_t& i, const size_t& j)
{
if (i == 0 || i > m_rows
|| j == 0 || j > m_cols)
{
// throw C++ error exception
throw out_of_range("Passed i and j values are out of range.");
}
else
return m_pdata[i - 1][j - 1];
}
void display()
{
cout << " ";
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
cout << m_pdata[h][k] << " ";
}
cout << endl;
}
cout << " ";
}
matrix<T> add(const matrix<T>& that)
{
matrix<T> answer(that.m_rows, that.m_cols);
for (size_t h = 0; h < m_rows; h++)
{
for (size_t k = 0; k < m_cols; k++)
{
// the resulting cumulative sum matrix element is
answer.m_pdata[h][k] =
// this matrix'es element
this->m_pdata[h][k]
// plus
+
// the other 'that' matrix'es element
that.m_pdata[h][k];
}
}
if (trace_me)
{
cout << "inside matrix<T>::add() function - answer is";
answer.display();
}
return answer;
}
matrix<T> multiply(const matrix<T> & that)
{
if (this->m_cols != that.m_rows)
{
// throw exception - matrix col-row incompatible
throw exception("matrix col-row incompatible");
}
// Output matrix C pg 205
// in Algorithm 4.3.19
matrix<T> answer(this->m_rows, that.m_cols);
// This the individual row of the answer matrix C
// that we iterate over
for (size_t row = 0; row < m_rows; row++)
{
for (size_t thiscol = 0; thiscol < this->m_cols; thiscol++)
{
// debug tracing to help
if (trace_me)
{
cout << "inside matrix<T>::multiply() function ";
answer.display();
}
// this is the cumulative multiplied sum
// Cij in Algorithm 4.3.19 page 205
// C(i,j) = 0
answer.m_pdata[row][thiscol] = 0;
for (size_t thatsrow = 0; thatsrow < that.m_rows; thatsrow++)
{
// cumulative inner loop
// multiplied sum for Cij
// Algorithm 4.3.19 page 205
// C(i,j) = C(i,j) + A(i,k) * B(k,j)
answer.m_pdata[row][thiscol] +=
this->m_pdata[row][thiscol] * that.m_pdata[thatsrow][thiscol];
}
}
}
if (trace_me)
{
cout << "Final resul to of matrix<T>::multiply() ";
answer.display();
}
// finally return the answer
return answer;
}
template< typename T>
friend std::ostream& operator<<(std::ostream& o, const matrix<T>& t);
template< typename T1>
friend std::istream& operator >> (std::istream& o, const matrix<T1>& t);
private:
// TO DO for us
// matrix<T> operator=(const matrix<T>&);
size_t m_rows = 0;
size_t m_cols = 0;
T** m_pdata = nullptr;
};
template< typename T1>
std::ostream& operator<<(std::ostream& o, const matrix<T1>& t)
{
for (size_t h = 0; h < t.m_rows; h++)
{
for (size_t k = 0; k < t.m_cols; k++)
{
o << t.m_pdata[h][k] << " ";
}
o << endl;
}
return o;
}
template< typename T1>
std::istream& operator >> (std::istream& o, const matrix<T1>& t)
{
// please place you code in basket
// place item in basket
for (size_t h = 0; h < t.m_rows; h++)
{
for (size_t k = 0; k < t.m_cols; k++)
{
o >> t.m_pdata[h][k];
}
}
return o;
}
Main_func.cpp
#include "matrix.h"
#include <ctime>
#include <string>
size_t sizemat = 6;
// debug flag
unsigned trace_me = 0;
int main(int argc, char* argv[])
{
using std::string;
if (argc > 1)
{
if (string(argv[1]) == "traceon")
trace_me = 1;
}
// set random seed
srand(time(NULL));
matrix<double> m1(sizemat, sizemat);
matrix<double> m2(sizemat, sizemat);
matrix<double> m3(sizemat, sizemat);
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), (double)(rand() % 10));
}
}
m1.display();
cout << " ===== calling getElement " << endl;
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
cout << m1.getElement((h + 1), (k + 1)) << " ";
}
cout << endl;
}
cout << endl << endl << m1 << endl;
// TESTING ADD FUNCTION
// how would we call add
matrix<double> result(sizemat, sizemat);
// fill m1 and m2 with data
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), 1);
m2.setElement((h + 1), (k + 1), 1);
}
}
// display m1 and m2
cout << "BEFORE ADDING m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
// Entering and displaying values for matrix m3 using overloaded operators >> and <<
cout << "Enter the values for matrix m3:" << endl;
std::cin >> m3;
cout << "m3 is ";
cout << m3;
// add the matrices
result = m1.add(m2);
cout << "AFTER ADDING ---- m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
cout << "result of adding m1 and m2 is: ";
result.display();
// TESTING MULTIPLY FUNCTION
matrix<double> mult_result(sizemat, sizemat);
for (size_t h = 0; h < sizemat; h++)
{
for (size_t k = 0; k < sizemat; k++)
{
m1.setElement((h + 1), (k + 1), 1);
m2.setElement((h + 1), (k + 1), 1);
}
}
// display m1 and m2
cout << "BEFORE MULTIPLY m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
// multiply matrices
mult_result = m1.multiply(m2);
cout << "AFTER MULTIPLY ---- m1 is: ";
m1.display();
cout << "m2 is: ";
m2.display();
cout << "result of multiplying m1 and m2 is: ";
mult_result.display();
return 0;
}
OUTPUT
5 7 7 6 0 5
7 9 8 6 6 4
9 6 9 0 2 2
1 6 0 2 8 8
7 2 1 4 9 2
1 3 1 9 6 0
===== calling getElement
5 7 7 6 0 5
7 9 8 6 6 4
9 6 9 0 2 2
1 6 0 2 8 8
7 2 1 4 9 2
1 3 1 9 6 0
5 7 7 6 0 5
7 9 8 6 6 4
9 6 9 0 2 2
1 6 0 2 8 8
7 2 1 4 9 2
1 3 1 9 6 0
BEFORE ADDING m1 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
m2 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
Enter the values for matrix m3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
m3 is
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
AFTER ADDING ----
m1 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
m2 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
result of adding m1 and m2 is:
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
BEFORE MULTIPLY m1 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
m2 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
AFTER MULTIPLY ----
m1 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
m2 is:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
result of multiplying m1 and m2 is:
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
6 6 6 6 6 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.