My program asks for the size of the square matrix (lets say 2 or 3). Then asks for parameter (a - addition, s-subtraction). Finally, the elements are filled using keyboard. Matrixes should be added/subtracted using overloaded operators (+= and -=, both
returning a matrix which contains the result) and displayed using <<.
<br/>
The problem I have is that when configuration is set to Debug, an error occurs - Debug Assertion Failed! .. Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).
If I use the Release mode, error doesnt occur, but instead, first 2 elements of the t table inside matrix class get corrupted (for given example, first cout<<"matrix 1:"<<a; call displays all elements fine, however 2nd call shows first
2 corrupted elements.
<br/>
After I did some debugging, it appears that both problems (error in Debug configuration, vaule changes in Release) occur once return str; gets called in << overloaded operator.
<br/>
Displaying the matrix using a.print() works fine.
<br/>
Complete code included below.
<div style="color:Black; background-color:White
<pre>#include <iostream>
<span style="color:Blue using <span style="color:Blue namespace std;
<span style="color:Blue class matrix
{
<span style="color:Blue int *t, size, dett;
<span style="color:Blue bool sym;
<span style="color:Blue static <span style="color:Blue int nr;
<span style="color:Blue public:
<span style="color:Blue void checksym()
{
sym=<span style="color:Blue true;
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
<span style="color:Blue if(i!=j && t[j+i*size]!=t[i+j*size])
sym=<span style="color:Blue false;
}
<span style="color:Blue void det()
{
<span style="color:Blue if(size==2)
dett=t[0]*t[3]-t[2]*t[1];
<span style="color:Blue else <span style="color:Blue if(size==3)
dett=t[0]*t[4]*t[8]+t[1]*t[5]*t[6]+t[2]*t[3]
*t[7]-t[6]*t[4]*t[2]-t[7]*t[5]*t[0]-t[8]*t[3]*t[1];
<span style="color:Blue else
{
cout<<<span style="color:#A31515 "Size higher than 3x3"<<endl;
dett=0;
}
}
<span style="color:Blue void fill()
{
cout<<<span style="color:#A31515 "Elements of matrix "<<nr<<<span style="color:#A31515 ": "<<endl;
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
cin>>t[j+i*size];
det();
checksym();
}
<span style="color:Blue void print()
{
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
{
cout<<endl;
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
cout<<t[j+i*size]<<<span style="color:#A31515 " ";
}
cout<<endl;
<span style="color:Blue if(sym)
cout<<<span style="color:#A31515 "Matrix is symmetric."<<endl;
<span style="color:Blue else
cout<<<span style="color:#A31515 "Matrix is not symmetric."<<endl;
cout<<<span style="color:#A31515 "Determinant: "<<dett<<endl;
}
matrix(<span style="color:Blue int s)
{
nr++;
size=s;
t=<span style="color:Blue new <span style="color:Blue int [s*s];
}
~matrix()
{
<span style="color:Blue delete [] t;
}
<span style="color:Blue int getsize() { <span style="color:Blue return size; }
<span style="color:Blue int getel(<span style="color:Blue int i) { <span style="color:Blue return t; }
<span style="color:Blue void setel(<span style="color:Blue int n, <span style="color:Blue int i) { t = n; }
<span style="color:Blue int getdet() { <span style="color:Blue return dett; }
<span style="color:Blue bool getsym() { <span style="color:Blue return sym; }
<span style="color:Blue friend matrix <span style="color:Blue operator+=(matrix, matrix);
<span style="color:Blue friend matrix <span style="color:Blue operator-=(matrix, matrix);
};
<span style="color:Blue int matrix::nr=0;
matrix <span style="color:Blue operator+=(matrix a, matrix b)
{
<span style="color:Blue int w=a.getsize();
matrix c(w);
<span style="color:Blue for(<span style="color:Blue int i=0;i<w;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<w;j++)
{
cout<<<span style="color:#A31515 "a.t["<<j+i*w<<<span style="color:#A31515 "]="<<a.t[j+i*w]<<endl;
cout<<<span style="color:#A31515 "b.t["<<j+i*w<<<span style="color:#A31515 "]="<<b.t[j+i*w]<<endl;
c.t[j+i*w] = a.t[j+i*w]+b.t[j+i*w];
}
<span style="color:Blue return c;
}
matrix <span style="color:Blue operator-=(matrix a, matrix b)
{
<span style="color:Blue int w=a.getsize();
matrix c(w);
<span style="color:Blue for(<span style="color:Blue int i=0;i<w;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<w;j++)
c.setel(a.getel(j+i*w)-b.getel(j+i*w),j+i*w);
<span style="color:Blue return c;
}
ostream & <span style="color:Blue operator<<(ostream & str, matrix a)
{
<span style="color:Blue int size=a.getsize();
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
{
str<<endl;
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
{
str<<a.getel(j+i*size)<<<span style="color:#A31515 " ";
}
}
str<<endl;
<span style="color:Blue if(a.getsym())
str<<<span style="color:#A31515 "Matrix is symmetric."<<endl;
<span style="color:Blue else
str<<<span style="color:#A31515 "Matrix is not symmetric."<<endl;
str<<<span style="color:#A31515 "Determinant: "<<a.getdet()<<endl;
<span style="color:Blue return str;
}
<span style="color:Blue void main()
{
<span style="color:Blue int w;
<span style="color:Blue char param;
cout<<<span style="color:#A31515 "Size of matrix: ";
cin>>w;
cout<<<span style="color:#A31515 "Parameter (a/s): ";
cin>>param;
<span style="color:Blue if(param != a && param != s)
{
cout<<<span style="color:#A31515 "Invaild parameter.";
<span style="color:Blue return;
}
matrix a(w);
a.fill();
matrix b(w);
b.fill();
<span style="color:Green /*
matrix c(w);
if(param=s)
c = a+=b;
else
c = a-=b;
*/
cout<<<span style="color:#A31515 "matrix 1:"<<a;
cout<<<span style="color:#A31515 "matrix 1:"<<a;
<span style="color:Green //cout<<"matrix 1:";
<span style="color:Green //a.print();
<span style="color:Green //cout<<"matrix 2:";
<span style="color:Green //b.print();
<span style="color:Green //cout<<"result matrix:";
<span style="color:Green //c.print();
<span style="color:Green //cout<<"matrix 1:"<<a;
<span style="color:Green //cout<<"matrix 2:"<<b;
<span style="color:Green //cout<<"result matrix:"<<c;
<span style="color:Green //system("pause");
}
[/code]
<br/>
View the full article
returning a matrix which contains the result) and displayed using <<.
<br/>
The problem I have is that when configuration is set to Debug, an error occurs - Debug Assertion Failed! .. Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).
If I use the Release mode, error doesnt occur, but instead, first 2 elements of the t table inside matrix class get corrupted (for given example, first cout<<"matrix 1:"<<a; call displays all elements fine, however 2nd call shows first
2 corrupted elements.
<br/>
After I did some debugging, it appears that both problems (error in Debug configuration, vaule changes in Release) occur once return str; gets called in << overloaded operator.
<br/>
Displaying the matrix using a.print() works fine.
<br/>
Complete code included below.
<div style="color:Black; background-color:White
<pre>#include <iostream>
<span style="color:Blue using <span style="color:Blue namespace std;
<span style="color:Blue class matrix
{
<span style="color:Blue int *t, size, dett;
<span style="color:Blue bool sym;
<span style="color:Blue static <span style="color:Blue int nr;
<span style="color:Blue public:
<span style="color:Blue void checksym()
{
sym=<span style="color:Blue true;
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
<span style="color:Blue if(i!=j && t[j+i*size]!=t[i+j*size])
sym=<span style="color:Blue false;
}
<span style="color:Blue void det()
{
<span style="color:Blue if(size==2)
dett=t[0]*t[3]-t[2]*t[1];
<span style="color:Blue else <span style="color:Blue if(size==3)
dett=t[0]*t[4]*t[8]+t[1]*t[5]*t[6]+t[2]*t[3]
*t[7]-t[6]*t[4]*t[2]-t[7]*t[5]*t[0]-t[8]*t[3]*t[1];
<span style="color:Blue else
{
cout<<<span style="color:#A31515 "Size higher than 3x3"<<endl;
dett=0;
}
}
<span style="color:Blue void fill()
{
cout<<<span style="color:#A31515 "Elements of matrix "<<nr<<<span style="color:#A31515 ": "<<endl;
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
cin>>t[j+i*size];
det();
checksym();
}
<span style="color:Blue void print()
{
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
{
cout<<endl;
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
cout<<t[j+i*size]<<<span style="color:#A31515 " ";
}
cout<<endl;
<span style="color:Blue if(sym)
cout<<<span style="color:#A31515 "Matrix is symmetric."<<endl;
<span style="color:Blue else
cout<<<span style="color:#A31515 "Matrix is not symmetric."<<endl;
cout<<<span style="color:#A31515 "Determinant: "<<dett<<endl;
}
matrix(<span style="color:Blue int s)
{
nr++;
size=s;
t=<span style="color:Blue new <span style="color:Blue int [s*s];
}
~matrix()
{
<span style="color:Blue delete [] t;
}
<span style="color:Blue int getsize() { <span style="color:Blue return size; }
<span style="color:Blue int getel(<span style="color:Blue int i) { <span style="color:Blue return t; }
<span style="color:Blue void setel(<span style="color:Blue int n, <span style="color:Blue int i) { t = n; }
<span style="color:Blue int getdet() { <span style="color:Blue return dett; }
<span style="color:Blue bool getsym() { <span style="color:Blue return sym; }
<span style="color:Blue friend matrix <span style="color:Blue operator+=(matrix, matrix);
<span style="color:Blue friend matrix <span style="color:Blue operator-=(matrix, matrix);
};
<span style="color:Blue int matrix::nr=0;
matrix <span style="color:Blue operator+=(matrix a, matrix b)
{
<span style="color:Blue int w=a.getsize();
matrix c(w);
<span style="color:Blue for(<span style="color:Blue int i=0;i<w;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<w;j++)
{
cout<<<span style="color:#A31515 "a.t["<<j+i*w<<<span style="color:#A31515 "]="<<a.t[j+i*w]<<endl;
cout<<<span style="color:#A31515 "b.t["<<j+i*w<<<span style="color:#A31515 "]="<<b.t[j+i*w]<<endl;
c.t[j+i*w] = a.t[j+i*w]+b.t[j+i*w];
}
<span style="color:Blue return c;
}
matrix <span style="color:Blue operator-=(matrix a, matrix b)
{
<span style="color:Blue int w=a.getsize();
matrix c(w);
<span style="color:Blue for(<span style="color:Blue int i=0;i<w;i++)
<span style="color:Blue for(<span style="color:Blue int j=0;j<w;j++)
c.setel(a.getel(j+i*w)-b.getel(j+i*w),j+i*w);
<span style="color:Blue return c;
}
ostream & <span style="color:Blue operator<<(ostream & str, matrix a)
{
<span style="color:Blue int size=a.getsize();
<span style="color:Blue for(<span style="color:Blue int i=0;i<size;i++)
{
str<<endl;
<span style="color:Blue for(<span style="color:Blue int j=0;j<size;j++)
{
str<<a.getel(j+i*size)<<<span style="color:#A31515 " ";
}
}
str<<endl;
<span style="color:Blue if(a.getsym())
str<<<span style="color:#A31515 "Matrix is symmetric."<<endl;
<span style="color:Blue else
str<<<span style="color:#A31515 "Matrix is not symmetric."<<endl;
str<<<span style="color:#A31515 "Determinant: "<<a.getdet()<<endl;
<span style="color:Blue return str;
}
<span style="color:Blue void main()
{
<span style="color:Blue int w;
<span style="color:Blue char param;
cout<<<span style="color:#A31515 "Size of matrix: ";
cin>>w;
cout<<<span style="color:#A31515 "Parameter (a/s): ";
cin>>param;
<span style="color:Blue if(param != a && param != s)
{
cout<<<span style="color:#A31515 "Invaild parameter.";
<span style="color:Blue return;
}
matrix a(w);
a.fill();
matrix b(w);
b.fill();
<span style="color:Green /*
matrix c(w);
if(param=s)
c = a+=b;
else
c = a-=b;
*/
cout<<<span style="color:#A31515 "matrix 1:"<<a;
cout<<<span style="color:#A31515 "matrix 1:"<<a;
<span style="color:Green //cout<<"matrix 1:";
<span style="color:Green //a.print();
<span style="color:Green //cout<<"matrix 2:";
<span style="color:Green //b.print();
<span style="color:Green //cout<<"result matrix:";
<span style="color:Green //c.print();
<span style="color:Green //cout<<"matrix 1:"<<a;
<span style="color:Green //cout<<"matrix 2:"<<b;
<span style="color:Green //cout<<"result matrix:"<<c;
<span style="color:Green //system("pause");
}
[/code]
<br/>
View the full article