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

Matrix plays an important role in computer graphics. Many industries like archit

ID: 3689879 • Letter: M

Question

Matrix plays an important role in computer graphics. Many industries like architecture, cartoon, automotive that were formerly done by hand drawing now are done routinely with the aid of computer graphics. Video gaming industry, maybe the earliest industry to rely heavily on computer graphics, is now representing rendered polygon in 3D. In video gaming industry, matrices are major mathematic tools to construct and manipulate a realistic animation of a polygonal figure. Examples of matrix operations include translations, rotations, and scaling. Other matrix transformation concepts like field of view, rendering, color transformation and projection. Understanding of matrices is a basic necessity to program 3D video games. In this lab assignment, you will define your own Matrix class to provide the basic matrix operation, such as matrix addition, Multiplication of a matrix by a number, multiplication of matrices, and so on. It cannot pass the compilation because the Matrix.cpp does not provide the complete implementation of the member functions declared in Matrix.h. In this Lab assignment, you need to provide the complete definition of the class named Matrix, which is used in the main function. You can either use the Matrix class declaration provided in Matrix.h or modify then provide your own declaration of the Matrix class. However, for this lab assignment, DO NOT modify main.cpp!!! 5. Create the Matrix.cpp file in your preferred text editor, and provide the implementation of member functions declared in Matrix.h file. First, you may need to open the Matrix.h file and take a look at the declaration of the class Matrix, if you want to use Matrix.h downloaded from step 3. Pay attention to the prototype of functions, which you need to overload operators such as “<<”, “()”, binary operators “+” and “*”, and so on. Then provide your own implementation in the Matrix.cpp file. 6. Create a makefile under your current working directory, which helps you generate the executable program named Lab11. Then run your executable program (Lab11) http://www.cs.uky.edu/~yipike/CS216S16/Lab11source.zip

Explanation / Answer

Matrix.cpp

#include <iostream>
#include <cassert>
#include "Matrix.h"

using namespace std;

//Constructor with two int parameters.
Matrix::Matrix(int sizeX, int sizeY) : dx(sizeX), dy(sizeY)
{
   assert(sizeX > 0 && sizeY > 0);
   p = new long*[dx];      
               // create array of pointers to long integers
   assert(p != 0);
   for (int i = 0; i < dx; i++)
   {      // for each pointer, create array of long integers
       p[i] = new long[dy];
       assert(p[i] != 0);
       for (int j = 0; j < dy; j++)
           p[i][j] = 0;
   }
}

Matrix::~Matrix()
{
   for (int i = 0; i < dx; i++)
       delete [] p[i];   // delete arrays of long integers
   delete [] p;   // delete array of pointers to long
}

long &Matrix::Element(int x, int y)
{
   assert(x >= 0 && x < dx && y >= 0 && y < dy);
   return p[x][y];
}

void Matrix::Print() const
{
   cout << endl;
   for (int x = 0; x < dx; x++)
   {
       for (int y = 0; y < dy; y++)
           cout << p[x][y] << " ";
       cout << endl;
   }
}

//Implement overloading for ()
long& Matrix::operator() (int x, int y){
   assert(x>=0 && x<dx && y>=0 && y<dy);
   return p[x][y];
}


//Implements copy Constructor
Matrix::Matrix(const Matrix &m) : dx(m.dx), dy(m.dy)
{
   p = new long*[dx];            // create array of pointers to long integers
   assert(p != 0);
   for (int i = 0; i < dx; i++)
   {
       p[i] = new long[dy]; // for each pointer, create array of l.i.s
       assert(p[i] != 0);
       for (int j = 0; j < dy; j++)
           p[i][j] = m.p[i][j];
   }
}


//Implement overloading assignment operator.
//This will make sure that = will copy the
//actual elements of a matrix and let the pointer
//point to the copied data
Matrix &Matrix::operator=(const Matrix &m)
{
   if (this != &m)
   {
       for (int i = 0; i < dx; i++)
           for (int j = 0; j < dy; j++)
               p[i][j] = m.p[i][j];
   }
   return *this;
}


//Overloading << operator
ostream &operator<<(ostream &out, const Matrix &m)
{
   out << endl;
   for (int x = 0; x < m.dx; x++)
   {
       for (int y = 0; y < m.dy; y++)
           out << m.p[x][y] << " ";
       out << endl;
   }
   return out;
}

//Overload * operator. int * Matrix form
Matrix operator*(int k, Matrix m1){
   Matrix mul(m1.dx, m1.dy);

   for( int i = 0; i < m1.dx; i ++ )
       for( int j = 0; j < m1.dy; j ++ )
           mul.p[i][j] = k* m1.p[i][j];

   return mul;
}

//Overload * operator, Matrix* int form
Matrix operator*(Matrix m1, int k){
   Matrix mul(m1.dx, m1.dy);

   for( int i = 0; i < m1.dx; i ++ )
       for( int j = 0; j < m1.dy; j ++ )
           mul.p[i][j] = m1.p[i][j]*k;

   return mul;
  
}

//Overloading + operator, Matrix + Matrix form
Matrix operator+(const Matrix m1, const Matrix m2){

   Matrix sum(m1.dx, m1.dy);

   //If two matrix does not have same dimension
   if( m1.dx != m2.dx || m1.dy != m2.dy)   return sum;

   for( int i = 0; i < m1.dx; i ++ )
       for( int j = 0; j < m1.dy; j ++ )
           sum.p[i][j] = m1.p[i][j] + m2.p[i][j];

   return sum;
}


//Overloading * operator for multiplying two matrix
Matrix operator*(const Matrix m1, const Matrix m2){
   Matrix mul(m1.dx, m1.dy);


   for( int i = 0; i < m1.dx; i ++ )
       for( int j = 0; j < m1.dy; j ++ )
           mul.p[i][j] = m1.p[i][j]* m2.p[i][j];

   return mul;
}

Matrix.h
#ifndef MATRIX_H
#define   MATRIX_H

class Matrix
{
public:

      //Overload << operator
      friend std::ostream& operator<<(std::ostream& os, const Matrix& m);

      //Overload * operator, int * Matrix form
      friend Matrix operator*(int i, Matrix m);

       //Overload * operator, Matrix* int form
      friend Matrix operator*(Matrix m, int i);

      //Overload * operator, Matrix * Matrix
      friend Matrix operator*(Matrix m1, Matrix m2);

      //Overload + operator, Matrix + Matrix
      friend Matrix operator+ (Matrix m1, Matrix m2);

  

   //Constructor
   Matrix(int sizeX, int sizeY);

   //copy constructor
   Matrix(const Matrix& m);

   //Overload Assignment operator
   Matrix& operator=(const Matrix& source);


   //Destructor
   ~Matrix();


   int GetSizeX() const { return dx; }
   int GetSizeY() const { return dy; }
   long &Element(int x, int y);        // return reference to an element
   void Print() const;

   //Overloading() operator
   long& operator() (int x, int y);

  


private:
   long **p;       // pointer to a pointer to a long integer
   int dx, dy;
};

#endif   /* MATRIX_H */

main.cpp

#include <cstdlib>
#include <iostream>
#include <cassert>
#include <ctime>
#include "Matrix.h"

using namespace std;

int main(int argc, char** argv) {
   int size1, size2, size3;
   const int RANGE = 5; //using to generate random number in the range[1,6]
      
   cout << "Please input three positive integers: (size)";
   cin >> size1 >> size2 >> size3;
   assert(size1 > 0 && size2 > 0 && size3 > 0);
  
   Matrix myMatrix1(size1, size2), myMatrix2(size2,size3);
   Matrix yourMatrix1(size1, size2), yourMatrix2(size2, size3);
   Matrix theirMatrix1(size1, size2), theirMatrix2(size1, size3);

   srand(time(0));
   for (int i = 0; i < size1; i++)
       for (int j = 0; j < size2; j++)
           myMatrix1(i,j) = rand() % RANGE + 1;
   yourMatrix1 = 2 * myMatrix1;
   theirMatrix1 = myMatrix1 + yourMatrix1;
   cout << "myMatrix1: " << endl;
   cout << myMatrix1 << endl << endl;
  
   cout << "yourMatrix1 = 2 * myMatrix " << endl;
   cout << yourMatrix1 << endl << endl;
  
   cout << "myMatrix1 + yourMatrix1: " << endl;
   cout << theirMatrix1 << endl << endl;
  
   for (int i = 0; i < size2; i++)
       for (int j = 0; j < size3; j++)
           myMatrix2(i,j) = rand() % RANGE + 1;
   yourMatrix2 = myMatrix2 * 3;
   theirMatrix2 = myMatrix1 * yourMatrix2;
   cout << "myMatrix1: " << endl;
   cout << myMatrix1 << endl << endl;
  
   cout << "myMatrix2: " << endl;
   cout << myMatrix2 << endl << endl;
  
   cout << "yourMatrix2 = myMatrix2 * 3 " << endl;
   cout << yourMatrix2 << endl << endl;
  
   cout << "myMatrix1 * yourMatrix2: " << endl;
   cout << theirMatrix2 << endl << endl;
  

   return 0;
}

sample output


Please input three positive integers: (size) 3 2 1                                                                                                          
myMatrix1:                                                                                                                                                  
                                                                                                                                                            
2       2                                                                                                                                                   
5       3                                                                                                                                                   
1       3                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
yourMatrix1 = 2 * myMatrix                                                                                                                                  
                                                                                                                                                            
4       4                                                                                                                                                   
10      6                                                                                                                                                   
2       6                                                                                                                                                   
myMatrix1 + yourMatrix1:                                                                                                                                    
                                                                                                                                                            
6       6                                                                                                                                                   
15      9                                                                                                                                                   
3       9                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
myMatrix1:                                                                                                                                                  
                                                                                                                                                            
2       2                                                                                                                                                   
5       3                                                                                                                                                   
1       3                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
myMatrix2:                                                                                                                                                  
2                                                                                                                                                           
4                                                                                                                                                           
                                                                                                                                                            
                                                                                                                                                            
yourMatrix2 = myMatrix2 * 3                                                                                                                                 
                                                                                                                                                            
6                                                                                                                                                           
12                                                                                                                                                          
                                                                                                                                                            
                                                                                                                                                            
myMatrix1 * yourMatrix2:                                                                                                                                    
                                                                                                                                                            
12                                                                                                                                                          
60                                                                                                                                                          
12