Question

C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

C++ CODE:

The matrix class consists of two files: matrix.h and matrix.cpp.

The class has the following definition:
matrix

-size:int

-mat:int**

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

+matrix(file:string)

+~matrix()

+operator

+(add:matrix*):matrix &

+operator-(sub:matrix*):matrix &

+friend operator<<(out:ostream &, t:const matrix&):ostream &

+displayRow(r:int):void

The class variables are as follows:

• size: the number of rows and columns in a square matrix

• mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2

• matrix(file:string): The default constructor. It receives the name of a file that it must then read to construct a matrix. The first line of the file contains the size of the matrix. The remainder of the file contains the matrix, in a row/column format. Each line will have a number of elements with no delimiters. Each value will be between 0 and 9.

• matrix(): The destructor for the class. It deallocates the memory of the class. It will also print out the following message with a new line at the end: matrix sum: X where X is the sum of all of the elements in the matrix.

• operator+(add:matrix*): An overload of the + operator. It will be used in conjunction with other matrices in the following way: A=A+B A 2,3 4,5 B 1,2 3,4 A=A+B 3,5 7,9 where A and B are both matrices. If the two matrices are of different sizes, then nothing happens and A should remain unchanged. If the matrices are same size, then there should be an element-wise addition and the result should be stored in A. This operation should be chainable, so the operator can be used in conjunction with others or itself.

• operator-(sub:matrix*): An overload of the - operator. It will be used in conjunction with other matrices in the following way: A=A-B A 2,3 4,5 B 1,2 3,4 3 A=A-B 1,1 1,1 where A and B are both matrices. If the two matrices are of different sizes, then nothing happens and A should remain unchanged. If the matrices are same size, then there should be an element-wise subtraction and the result should be stored in A. This operation should be chainable, so the operator can be used in conjunction with others or itself.

• friend operator<<(out:ostream &, t:const matrix&): This overload of the << operator should display the entire matrix as is in the following format: 1,2,3 4,5,6 7,8,9 From each row, each element should be displayed with comma delimiters and a new line at the end.

• displayRow(r:int): This function receives a row (indexed from 0), and displays it in the following format: 1,2,3 From the appropriate row, each element should be displayed with comma delimiters and a new line at the end.

Your submission must contain matrix.h, matrix.cpp,main.cpp,mat.txt,mat2.txt . You will only need the iostream,sstream and fstream libraries for this

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

//matrix.h
class Matrix
{
public:
   Matrix();
   Matrix(string file);
   ~Matrix();
   Matrix& operator + (const Matrix& add);
   Matrix& operator - (const Matrix& add);
   friend ostream& operator << (ostream& out, const Matrix& mat);
   void displayRow(int row);
private:
   int size;
   int** mat;
};

//matrix.cpp
Matrix::Matrix()
{
   size = 0;
   mat = nullptr;
}

Matrix::Matrix(string file)
{
   ifstream fin;
   fin.open(file);

   if (!fin.is_open())
       return;

   fin >> size;

   mat = new int*[size];

   for (size_t i = 0; i < size; i++)
   {
       mat[i] = new int[size];
   }

   string in;
   int row = 0, column = 0, count = 0;

   //reading the new line
   char ch;
   fin.get(ch);

   while (std::getline(fin, in))
   {
       stringstream ss(in);
       int num;
       while (ss >> num)
       {
           mat[row][column] = num;
           ++column;
       }
       column = 0;
       ++row;
   }

}

Matrix::~Matrix()
{
   for (size_t i = 0; i < size; i++)
   {
       delete[] mat[i];
   }

   delete[] mat;
   size = 0;
}

Matrix & Matrix::operator+(const Matrix& add)
{
   if (this->size == add.size)
   {
       for (size_t i = 0; i < size; i++)
       {
           for (size_t j = 0; j < size; j++)
           {
               this->mat[i][j] += add.mat[i][j];
           }
       }
   }

   return *this;
}

Matrix & Matrix::operator-(const Matrix& sub)
{
   if (this->size == sub.size)
   {
       for (size_t i = 0; i < size; i++)
       {
           for (size_t j = 0; j < size; j++)
           {
               this->mat[i][j] -= sub.mat[i][j];
           }
       }
   }
  
   return *this;
}

void Matrix::displayRow(int row)
{
   for (size_t i = 0; i < size; i++)
   {
       cout<< this->mat[row][i];
       if (i != (size - 1))
           cout << ",";
   }
   cout << "\n";
}

ostream & operator<<(ostream & out, const Matrix & mat)
{
   for (size_t i = 0; i < mat.size; i++)
   {
       for (size_t j = 0; j < mat.size; j++)
       {
           out << mat.mat[i][j];
           if (j != (mat.size - 1))
               out << ",";
       }
       cout << "\n";
   }
   cout << "\n";

   return out;
}

//main.cpp
int main()
{

Matrix m1("m1.txt");
   Matrix m2("m2.txt");

   Matrix m3, m4;

   m3 = m1 + m2;

   cout << m3;

   Matrix m5("m1.txt");

   m4 = m5 - m2;

   cout << m4;

   m2.displayRow(1);

   return 0;

}

Output:

m1.txt

m2.txt

Add a comment
Know the answer?
Add Answer to:
C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Implement a C++ class to model the mathematical operations of a matrix. Your class should include...

    Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which adds two matrices: power() which raises the first matrix to power n: "" which returns true if both matrices are equal. You need to overload the C++ equality operator. A sample run follows. Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of matrix 1 row by row: 1 5 0 1 3...

  • C++,please help. I have no idea how to do that begining with zero parameter constructor. and...

    C++,please help. I have no idea how to do that begining with zero parameter constructor. and help me to test the method, Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a...

    NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT