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

Answer in C++ CSC331 project In this project you will be defining a Template Mat

ID: 3910157 • Letter: A

Question

Answer in C++

CSC331 project 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 A variable to represent rows for the number of rows of the matrix A variable column for the number of columns of the matrix A pointer variable data to point to the matrix data. 1. 2. 3. The Matrix class must also have the following member functions. 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. 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. A corect destructor. A method called display) that displays the matrix in proper row/column form. 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 The method set Element ( inti, intj. int value) that set the (ii) element of the matmx to the value. Include exception handling with object out of range for the case when i or j or both are out of bounds. 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) 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 1. 2. 3. 4. 5. 6. 7? 8. 205 of your textbook 9. Overload the out stream operatorfor 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 parametens 4. Use the opcrator "" 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.

Explanation / Answer

ScreenShot

----------------------------------------------------

Matrix.h

//Header files for I/O and random generation
#include<cstdlib>
#include<iostream>
using namespace std;
//Matrix class
template<class T>
class Matrix {
   //Member variables
   int col, row;
   T *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;
       }
   }
  
   //<<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) {
       for (int i = 0; i < m.row; i++) {
           for (int j = 0; j < m.col; j++) {
               cout << "Enter elements:";
               in >> m.data[i*m.col + j];
              
           }
          
       }

       return in;
   }

   /*//Add matrix
   Matrix<int> add(Matrix a) {
       Matrix<int> addition;
       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 (int i = 0; i <row; i++)
           {
               for (int j = 0; j <col; j++)
               {
                   addition.data[i *col + j]) = *(data + i * col + j) + a.data[i *col + j];
               }
           }
       }
       return addition;
   }
   Matrix<int> multiply(Matrix a) {
   Matrix<int> multiply;
   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(int i = 0; i < row; i++) {
               for (int j = 0; j < a.col; j++) {
                   multiply.data[i *col + j])=0;
                   for(int k = 0; k < col; k++)
                       multiply.data[i *col + j])=*(data + i * col + k) *a.data[k *col + j];
                  
               }
               return multiply
           }*/
  
   //Destructor
   ~Matrix()
   {
       delete[] data;
   }
};

Main

//Header file
#include "matrix.h"
//Main method
int main()
{
   //Matrix object creation
   Matrix<int> m1;
   Matrix<int> m2;
   //Set elemnt with new value
   m1.setElement(2, 2, 1);
   m2.setElement(2, 2, 1);
   //Get element in given position
   m1.getElement(2, 2);
   m2.getElement(2, 2);
   //Check exception
   m1.setElement(3, 2, 1);
   m2.getElement(3, 2);
   //overload <<operator to print out
   cout << "First matrix: "<<m1;
   cout << "Second matrix: "<<m2;

   //overload >>operator to inputting
   cin >> m1;
   //Display new matrix
   cout << "Matrix: " << m1;
  
    return 0;
}

----------------------------------

Output

Element in matrix[2,2]=1
Element in matrix[2,2]=1
Out of bound
Out of bound
First matrix:
2 8 5
1 10 5
9 9 1
Second matrix:
5 6 6
2 8 2
2 6 1
Enter elements:1
Enter elements:2
Enter elements:3
Enter elements:4
Enter elements:5
Enter elements:6
Enter elements:7
Enter elements:8
Enter elements:9
Matrix:
1 2 3
4 5 6
7 8 9
Press any key to continue . . .