Question
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 Matrix a. Private Members i. int rows; ii. int cols; ii. double data; b. Provide NO default constructor c. Constructor i. Matrix(int,int)takes rows and cols ii. Inside constructor allocate and initialize data d. Destructor i. Matrix0 ii. Inside destructor delete memory allocation of data completely 2. [10pts] Overload Copy constructor 3. [10pts] ad<<and >> a. For input, user inputs the values of the matrix in row then column format i. HINT: using a space you can separate column data and enter for row b. For output, use tab and return line to create a matrix-like output 4. [10pts] Overload the assignment operator (). 5. [20pts] Overload (+), ),(), and (-) operators and () for post and pre type 6. [30pts] Overload the () operator to perform matrix multiplication. a. Check the dimensions of both matrices to ensure proper dimensions, output an error on cerr and exit(1) if dimensions are invalid. b. Must be able to work on any 2 valid matrices. 7. [10pts] Program Correctness a. Prompt user for number rows and columns for two matrices. b. Prompt the user to enter data for these matrices.
media%2Fe4a%2Fe4a63f13-bbac-4271-9ebe-9c
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <stdlib.h>
using namespace std;
class Matrix
{
private:
int rows;
int cols;
double** data;
public:
Matrix(int r, int c)
{
rows = r;
cols = c;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
}
Matrix()
{
rows = 0;
cols = 0;
}
~Matrix()
{
delete[] data;
}
Matrix(const Matrix &m)
{
rows = m.rows;
cols = m.cols;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
data[i][j] = m.data[i][j];
}
}
}
void operator = (const Matrix &m )
{
rows = m.rows;
cols = m.cols;
data = new double* [rows];
for(int i = 0; i < rows; i++)
{
data[i] = new double[cols];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
data[i][j] = m.data[i][j];
}
}
  
}
Matrix operator+(const Matrix& m2);
Matrix operator-(const Matrix& m2);
friend ostream & operator << (ostream &out, const Matrix &m);
friend istream & operator >> (istream &in, Matrix &m);
friend Matrix operator*(Matrix m1, const Matrix& m2); };
//overloading << operator ostream & operator << (ostream &out, const Matrix &m)
{
out << "value of the matrix is"<<endl;
for(int i = 0; i < m.rows; i++)
{
for(int j =0; j < m.cols; j++)
{
out << m.data[i][j]<<"\t";
  
}
out<<endl;
}
return out;
  
}
//overloading >> operator istream & operator >> (istream &in, Matrix &m)
{
cout << "Enter the matrix values"<<endl;
for(int i = 0; i < m.rows; i++)
{
for(int j =0; j < m.cols; j++)
{
in >> m.data[i][j];
}
}
return in;
  
}
//overloading * operator Matrix operator*(Matrix m1, const Matrix& m2)
{ if(m1.cols != m2.rows)
{
cerr << "invalid dimensions: Mutiplication cannot be performed"<<endl; exit(1);
}
Matrix result(m1.rows, m2.cols);
for (int i = 0; i < m1.rows; i++)
{
for (int j = 0; j < m2.cols; j++)
{
for (int k = 0; k < m1.cols; k++)
{
result.data[i][j] += m1.data[i][k]*m2.data[k][j];
}
}
}
return result;
  
}
//overoad + operator Matrix Matrix::operator+(const Matrix& m2)
{
if((this->rows != m2.rows) || (this->cols != m2.cols))
{
cerr << "invalid dimensions: Number of rows and columns must be equal for both matrix"<<endl; exit(1);
}
Matrix result(rows,cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result.data[i][j] = this->data[i][j]+m2.data[i][j];
}
  
}
return result;
}
Matrix Matrix::operator-(const Matrix& m2) {
if((this->rows != m2.rows) || (this->cols != m2.cols)
{
cerr << "invalid dimensions: Number of rows and columns must be equal for both matrix"<<endl; exit(1);
}
Matrix result(rows,cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0;
j < cols; j++)
{
result.data[i][j] = this->data[i][j]-m2.data[i][j];
}
}
return result;
} int main()
{
Matrix m3; int r, c; cout << "enter number of rows and columns for first matrix"<<endl; cin >> r >> c; Matrix m1(r,c); //calls overloaded >> operator cin >> m1;
cout << "enter number of rows and columns for Second matrix"<<endl;
cin >> r >> c; Matrix m2(r,c); //calls overloaded >> operator cin >> m2;
//calls overloaded operator * m3 = m1 * m2;
//calls overloaded << operator cout << m3; //calls overloaded + operator m3 = m1 + m2;
cout << m3; //calls overloaded - operator m3 = m1 - m2;
cout << m3;
return 0;
  
}

Add a comment
Know the answer?
Add Answer to:
Please help me out with this assignment. Please read the requirements carefully. And in the main...
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
  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide 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...

  • 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...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • I need help modifying this program. How would I make sure that the methods is being...

    I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is   //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...

    Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

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