In this project you will be defining a Template Matrix class in C++ and equip th
ID: 3909682 • Letter: I
Question
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.
2 files should be submitted
matrix.h - containing template matrix class and functions
main_function.cpp - containing the main function
Both files should contain lots of documentation of what the code is doing
Explanation / Answer
Screenshot
Program
matrix.h
//Header files for I/O and random generation
#include<cstdlib>
#include<iostream>
using namespace std;
//Matrix class
class Matrix {
//Member variables
int col, row;
int *data;
//Member functions
public:
//Default constructor
Matrix() {
row = 3;
col = 3;
data = new int[row*col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
*(data+i*col+j) = (rand() % 10) + 1;
}
}
}
//Parameterised constructor
Matrix(int r,int c) {
row = r; col = c;
data = new int[row*col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
*(data + i * col + j) = (rand() % 10) + 1;
}
}
}
//Display matrix
void display() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << *(data + i * col + j) << " ";
}
cout << endl;
}
}
//Get elemnt from a position
void getElement(int i, int j) {
if (i < row && j < col) {
cout << "Element in matrix[" << i << "," << j << "]=" << *(data + i * col + j) << endl;
}
else {
cout << "Out of bound" << endl;
}
}
//Set element in matrix
void setElement(int i, int j, int value) {
if (i < row && j < col) {
*(data + i * col + j) = value;
}
else {
cout << "Out of bound" << endl;
}
}
//Add matrix
Matrix add(Matrix a) {
Matrix ans;
int i = 0, j = 0;
if (row != a.row || col != a.col)
{
cout<<"ERROR: The two matrices should have same no. of rows and columns for addition!"<<endl;
}
else
{
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
ans.data [ i * col + j]=(*(data+ i * col + j)+a.data [ i * col + j]);
}
}
}
return ans;
}
//Multiply matrix
Matrix multiply(Matrix a) {
Matrix ans;
int i = 0, j = 0,k=0;
if (col != a.row )
{
cout << "ERROR: The two matrices should have same no. of rows and columns for addition!" << endl;
}
else
{
for (i = 0; i<row; ++i)
{
for (j = 0; j<a.col; ++j)
{
ans.data[i *a.col + j] = 0;
for (k = 0; k<col; ++k)
ans.data[i *a.col + j] = ans.data[i *a.col + j] + (*(data + i * col + j) * a.data[i * col + j]);
}
}
}
return ans;
}
//<<operator overloading
friend ostream & operator << (ostream &out, const Matrix &m) {
for (int i = 0; i < m.row; i++) {
for (int j = 0; j < m.col; j++) {
out << m.data[i*m.col+j] << " ";
}
out << endl;
}
return out;
}
//>>operator overloading
friend istream & operator >> (istream &in, Matrix &m) {
cout << "Enter number of rows";
in >> m.row;
cout << "Enter number ofcols";
in >> m.col;
return in;
}
//Destructor
~Matrix()
{
delete[] data;
}
};
-------------------------------
main_function.cpp
//Header file
#include "matrix.h"
//Main method
int main()
{
//Matrix object creation
Matrix m;
//Display matrix
m.display();
//Get element in given position
m.getElement(2, 2);
//Set elemnt with new value
m.setElement(2, 2,1);
m.display();
//For overload check
Matrix m1;
cin >> m1;
cout << m1;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.