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;
}
Please help me out with this assignment. Please read the requirements carefully. And in the main...
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 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 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, 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 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 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 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 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 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 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...