I am trying to write a class that do matrix multiplication. The
class matrix declared like below:
class matrix
{
public:
vector<vector<int> > M;
matrix();
matrix(int m, int n);
matrix(vector<vector<int> > &m);
matrix Mul(matrix m1);
void Inverse();
bool SquareMatrix();
void GetDet();
int Det(vector<vector<int> > &m);
void Print();
};
I initialize and enter the elements of the matrix M in this constructor:
matrix::matrix(int m, int n)
{
vector<vector<int> > M(m, vector<int>(n, 0));
cout << "Enter the elements: " << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> M[i][j];
}
}
}
However, the member function "Mul" do not receive the data I input through the constructor.
matrix matrix::Mul(matrix m1)
{
int **ppInt;
ppInt = new int *[M.size()];
for (int i = 0; i < M.size(); i++)
{
ppInt[i] = new int[m1.M[0].size()];
}
if (M[0].size() != m1.M.size())
{
cout << "Cannot do multiplication!" << endl;
return matrix();
}
else
{
for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < m1.M[0].size(); j++)
{
int ele_buf = 0;
for (int k = 0; k < M[0].size(); k++)
{
ele_buf += M[i][k] * m1.M[k][j];
}
ppInt[i][j] = ele_buf;
}
}
}
int d1 = M.size(), d2 = m1.M[0].size();
for (int i = 0; i < M.size(); i++)
{
M[i].clear();
}
M.clear();
for (int i = 0; i < d1; i++)
{
for (int j = 0; j < d2; j++)
{
M[i][j] = ppInt[i][j];
}
}
}
How can I fix it?
Please let me know if my problem is not clear enough.
I initialize and enter the elements of the matrix M in this
constructor!
No! You are working with a local M, not with the member M.
In the constructor
matrix::matrix(int m, int n)
{
vector<vector<int> > M(m, vector<int>(n, 0)); // local to the constructor only!
// .........
}
the M is local to the scope of the constructor, which shadows the member you defined with the same name M. Therefore it (i.e the one in the constructor) will be destroyed after the constructor scope. In effect, the member M will never get initialized.
How can I fix it?
In order to fix, you might want to std::vector::resize the member M
matrix(int m, int n)
{
M.resize(m, vector<int>(n, 0)); // resize M with `m` and `n`
std::cout << "Enter the elements: " << std::endl;
for (std::vector<int>& row : M)
for (int& ele : row)
std::cin >> ele; // fill the elements
}
I initialize and enter the elements of the matrix M in this constructor:
No you don't. You are initializing a vector called M that shadows the member with the same name. The M in the constructor is a different vector that is local to the constructor.
To initialize members, use the initializer list:
matrix::matrix(int m, int n) : M(m, vector<int>(n, 0)) {
cout << "Enter the elements: " << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> M[i][j];
}
}
}
Related
This question already has answers here:
What is The Rule of Three?
(8 answers)
Closed 2 years ago.
I am trying to write a matrice class in c++ with a overloaded + operator that is a friend function for summing a matrix with a number, ie sum every element of the matrice with the number.
for example:-
2+ |1|2| = |3|4|
|2|1| |4|3|
main.cpp :-
#include <iostream>
using namespace std;
class matrice {
int** a;
int rows, columns, i, j;
public:
matrice() {}
matrice(matrice& A) { //copy constructor for deep copying the result of 2+A into C
rows = A.rows;
columns = A.columns;
a = new int* [rows];
for (i = 0; i < rows; i++) {
a[i] = new int[columns];
*a[i] = 0;
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
a[i][j] = A.a[i][j];
}
}
}
matrice(int m, int n) {
rows = m;
columns = n;
a = new int* [rows];
for (i = 0; i < rows; i++) {
a[i] = new int[columns];
*a[i] = 0;
}
}
~matrice() {
delete[] a;
}
void insert(int p, int q, int value) {
a[p][q] = value;
}
void print() {
for (i = 0; i < rows; i++) {
cout << "\n";
for (j = 0; j < columns; j++) {
cout << a[i][j] << " ";
}
}
}
friend matrice operator+(int k, matrice& A);
friend matrice operator+(matrice& A, int k);
};
matrice operator+(int k, matrice& A) { //for performing 2+A
matrice temp(A.rows, A.columns);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.columns; j++) {
temp.a[i][j] = A.a[i][j] + k;
}
}
return temp;
}
matrice operator+(matrice& A, int k) { //for performing A+2
matrice temp(A.rows, A.columns);
for (int i = 0; i < A.rows; i++) {
for (int j = 0; j < A.columns; j++) {
temp.a[i][j] = A.a[i][j] + k;
}
}
return temp;
}
int main() {
int i, j, m, n, value;
cout << "\nEnter order of A matrice:";
cin >> m >> n;
matrice A(m, n);
cout << "\nEnter the matrice:";
for (i = 0; i < m; i++) {
cout << "\nEnter row " << i + 1 << " : ";
for (j = 0; j < n; j++) {
cin >> value;
A.insert(i, j, value);
}
}
cout << "\nThe entered matrice is :";
A.print();
cout << "\n\n";
cout << "\nEnter order of B matrice:";
cin >> m >> n;
matrice B(m, n);
cout << "\nEnter the matrice:";
for (i = 0; i < m; i++) {
cout << "\nEnter row " << i + 1 << " : ";
for (j = 0; j < n; j++) {
cin >> value;
B.insert(i, j, value);
}
}
cout << "\nThe entered matrice is :";
B.print();
cout << "\n\ntesting 2+A";
matrice C; //Everything upto here is fine
C = A + 2; // C.a is pointing to unreadable memory because of destruction after doing A+2
C.print(); // so access violation error
}
The problem is that the destructor of C is called after invoking the copy constructor which causes the double pointer a in C to be deleted. So when C.print() is called An Access violation reading location 0xDDDDDDDD exception is thrown.
I can't find out why C's destructor is called.
The rule of 5 says that if you manually handle allocations, you should have an explicit (or explicitely deleted):
copy constructor
copy assignment operator
move constructor
move assignement operator
destructor
You might omit the ones that you are sure that you code will never use, but it is highly dangerous.
Anyway correct allocation/de-allocation handling is complex and requires very cautious code. For example the correct declaration for a copy ctor should be matrice(const matrice& A), and comments already warned you of memory leaks.
So my advice is:
if you want to learn about manual allocation management, follow best practices: rule of 5 an copy/swap idiom to avoid caveats
if you just want working code, replace 1D dynamic arrays with vectors and let the standard library handle the corner cases
A great deal of unexpected numbers outputted on my console window when I compiled and ran the program.
The class "matrix" is declared as below:
class matrix
{
public:
vector<vector<int> > M;
matrix();
matrix(int m, int n);
matrix(vector<vector<int> > &m);
matrix Mul(matrix m1);
void Inverse();
bool SquareMatrix();
void GetDet();
int Det(vector<vector<int> > &m);
void Print();
};
the member function "Mul":
matrix matrix::Mul(matrix m1)
{
vector<vector<int> > Temp(M.size(), vector<int>(m1.M[0].size(), 0));
if (M[0].size() != m1.M.size())
{
cout << "Cannot do multiplication!" << endl;
return matrix();
}
else
{
for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < m1.M[0].size(); j++)
{
int ele_buf = 0;
for (int k = 0; k < M[0].size(); k++)
{
ele_buf += M[i][k] * m1.M[k][j];
}
Temp[i][j] = ele_buf;
}
}
}
int d1 = M.size(), d2 = m1.M[0].size();
for (int i = 0; i < M.size(); i++)
{
M[i].clear();
}
M.clear();
M.resize(Temp.size(), vector<int>(Temp[0].size(), 0));
for (int i = 0; i < d1; i++)
{
for (int j = 0; j < d2; j++)
{
M[i][j] = Temp[i][j];
}
}
}
M[i][j] hold the expected value at this point.
the member function "Print":
void matrix::Print()
{
for (int i = 0; i < M.size(); i++) {
for (int j = 0; j < M[0].size(); j++) {
cout << M[i][j] << " ";
}
cout << endl;
}
}
Output wrong M.
main:
/*
call constructor to initialize m1 and m2
*/
//...
matrix m3 = m1.Mul(m2);
m3.Print();
//...
How can I fix it?
I'm new here, plz let me know if I didn't make my question clear.
Editted:
matrix matrix::Mul(matrix m1)
{
vector<vector<int> > Temp(M.size(), vector<int>(m1.M[0].size(), 0));
if (M[0].size() != m1.M.size())
{
cout << "Cannot do multiplication!" << endl;
return matrix();
}
else
{
//TO_DO: Multiply two matrixes and print the result.
for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < m1.M[0].size(); j++)
{
int ele_buf = 0;
for (int k = 0; k < M[0].size(); k++)
{
ele_buf += M[i][k] * m1.M[k][j];
}
Temp[i][j] = ele_buf;
}
}
}
int d1 = M.size(), d2 = m1.M[0].size();
for (int i = 0; i < M.size(); i++)
{
M[i].clear();
}
M.clear();
return matrix(Temp);
}
Functionning properly,
thanks.
You forgot to add a return statement, which is trivial to fix. However, your code is not written in a good OO manner, so here come few suggestions for the improvement:
don't pass matrix m1 by value, because you are paying an expensive copy each time you invoke this function; use const matrix& m1 instead
in the body of Mul you are modifying member M, which is a serious side effect; remove the part of code:
int d1 = M.size(), d2 = m1.M[0].size();
for (int i = 0; i < M.size(); i++)
{
M[i].clear();
}
M.clear();
instead of implementing a separate function called Mul, overload the * operator (see e.g. here and here)
You are promised to return matrix from the Mul function
matrix matrix::Mul(matrix m1);
^^^^^^
and you did not keep the promise. This can cause undefined behaviour to your program.
Add an proper return statement in the function.
matrix matrix::Mul(matrix m1)
{
// ... code
return *this; // matrix(Temp) itself
}
My code stores integers in a [n] x [n] matrix. But when I use my printMatrix() method, it returns only zeros on command prompt, instead of integers I previously assigned to the matrix. I think I have to use reference/pointers but I'm not quite sure how. Here is my code:
#include <iostream>
#include <vector>
using namespace std;
class Matrices
{
public:
Matrices();
void storeMatrix(vector <vector <int> > matr, int n);
void printMatrix(vector <vector <int> > matr, int n);
private:
int n;
vector <vector <int> > matr;
int cell;
};//class Matrices
Matrices::Matrices()
{
}//Constructor
void Matrices::storeMatrix(vector <vector <int> > matr, int n)
{
for( int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << "Please insert an int for matr [" << i << "] [" << j << "]" << endl;
cin >> cell;
matr[i][j] = cell;
}
}
}//storeMatrix
void Matrices::printMatrix(vector <vector <int> > matr, int n)
{
for( int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << matr[i][j];
}
cout<<endl;
}
}//printMatrix
int main()
{
int m;
cin >> m;
int n = 1;
for (int i = 1; i <= m; i++) // loop to make the input integer equivalent to power of 2;
n = n * 2;
vector <vector <int> > matr (n , vector <int> (n));
Matrices k;
k.storeMatrix(matr, n);
k.printMatrix(matr, n);
return 0;
}
Any ideas how to fix it ?
Remove all the parameters related to printMatrix(). In main() and in its prototype(within the class) and from its definition void YourClassName::printMatrix(). and then inside its definition set this:
cout << this->matr[i][j]; // see below for further explanation
instead of this:
cout << matr[i][j];
Adding This will print the private member of "this current instance" that you already filled with data in the previous function.
Also in your store() you need to "link" what you are doing with the current instance.
Overall: This design should help you understand better what is going on. See this main():
int main()
{
int m;
int n = 1;
cin >> m;
Matrices k(m, n); // create an object k
k.storeMatrix(); // store matrix for "this" object k
k.printMatrix(); // print matrix of "this" object k
return 0;
}
And look at the class:
class Matrices
{
public:
Matrices();
Matrices(int, int);
void storeMatrix();
void printMatrix();
private:
int n;
vector <vector <int> > matr;
int cell;
};//class Matrices
Matrices::Matrices()
{}
Matrices::Matrices(int m, int n)
{
for (int i = 1; i <= m; i++) // loop to make the input integer equivalent to power of 2;
n = n * 2;
vector <vector <int> > _matr (n, vector <int>(n));
this->matr = _matr;
}//Constructor
void Matrices::storeMatrix()
{
for (int i = 0; i < this->matr.size(); i++)
{
for (int j = 0; j < this->matr[i].size(); j++)
{
cout << "Please insert an int for matr [" << i << "] [" << j << "]" << endl;
cin >> cell;
this->matr[i][j] = cell; //whatever "this" object is, store its matrix
}
}
}//storeMatrix
void Matrices::printMatrix()
{
for (int i = 0; i < this->matr.size(); i++)
{
for (int j = 0; j < this->matr[i].size(); j++)
{
cout << this->matr[i][j]; // whatever "this" objecct is, print its matrix
}
cout << endl;
}
}//printMatrix
I'm trying to implement a matrix class and overloading the + and = operators.
The problem is I'm getting weird output when I add two matrices as in this picture.
adding two matrices output console
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
int **p, m, n;
public:
Matrix(int row, int col)
{
m = row;
n = col;
p = new int*[m];
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
Matrix (Matrix & x)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j]=x.p[i][j];
}
}
}
~Matrix()
{
for (int i = 0; i < m; i++)
delete [] p[i];
delete [] p;
}
void accept()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cout << setw(10)<<left <<p[i][j] <<" | ";
}
cout << "\n--------------------------------------"<<endl;
}
}
Matrix& operator +(const Matrix & m2)
{
Matrix r(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
r.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return r;
}
Matrix& operator= (const Matrix & eq)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j]=eq.p[i][j];
}
}
return *this;
}
friend Matrix operator * (Matrix, Matrix);
};
Matrix operator* (Matrix a , Matrix b)
{
Matrix B(1,1);
if(a.n == b.m)
{
Matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
B = T;
}
return B;
}
int main()
{
cout << "Enter Matrix 1 (3x2):"<<endl;
Matrix m1(3,2);
m1.accept();
m1.display();
cout << "Enter Matrix 2 (3x2):"<<endl;
Matrix m2(3,2);
m2.accept();
m2.display();
Matrix m3(3,2);
m3=m1+m2;
cout <<endl<< "matrix1 + matix2 is:\n "<<endl;
m3.display();
}
Any ideas how to fix that? I would be grateful for your help and advices with improving it because probably there will be some mistakes.
I use CodeBlocks IDE.
The problem is that your operator + returns a local variable Matrix as a reference; this is undefined behavior. It should be returned by value, not by reference.
Make sure that your Matrix class has a copy constructor that takes a const input, i.e.
Matrix (const Matrix & x)
and that it initializes the array before writing into it. Move the initialization code (the two loops with allocation) into a separate private function, and call it from the default constructor and from the copy constructor.
I have a program that is suppose to add together two matrices but when it gets to the add part in the main it just gets stuck and does nothing. I have messed with this for quite some time but to no avail. Any help or recommendations would be appreciated.
#include <iostream>
using namespace std;
class matrix
{
private:
int **p, m, n;
public:
matrix(int row, int col)
{
m = row;
n = col;
p = new int*[m];
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
~matrix()
{
for (int i = 0; i < m; i++)
delete p[i];
delete p;
}
void fill()
{
cout<<"Enter the matrix elements:";
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
cout <<"The matrix is:";
for(int i = 0; i < m; i++)
{
cout << endl;
for(int j = 0; j < n; j++)
{
cout << p[i][j] <<" ";
}
}
cout << endl;
}
matrix operator +(matrix m2)
{
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return T;
}
matrix operator =(matrix eq)
{
m = eq.m;
n = eq.n;
p = eq.p;
return *this;
}
friend matrix operator *(matrix, matrix);
};
matrix operator *(matrix a , matrix b)
{
matrix B(1,1);
if(a.n == b.m)
{
matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
B = T;
}
return B;
}
int main()
{
matrix a(3,3), b(3,3);
a.fill();
a.display();
b.fill();
b.display();
cout << "addition of a and b\n";
b = b + a;
b.display();
cout << "multiplication of a and b\n";
b = (a * b);
b.display();
}
Your program is violating the rule of big three: it has a destructor but no assignment operator and no copy constructor. It keeps the data using raw pointers, but it's not managing proper ownership with copies are done and assignments are performed.
When your matrix class is copied and assigned your program is entering the Undefined Behavior territory so anything can happen. In this code copy construction is done implicitly when passing a matrix parameter by value, and assignment is done explicitly in main.