C++/2D Array
Expert: Zlatko - 2/8/2010
QuestionQUESTION: Hi Zlatko
The outputs are incorrect. I am trying to perform +,-,*,/ for 2D arrays.
Can advise me the correct way to do it.
[Code]
#include <iostream>
using namespace std;
//member function of matrix class
class matrix{
public:
matrix();
matrix(int m,int n);
int getRow();
int getCol();
double& operator()(int, int);
//the operator used in the matrix class which are (addition operator, multiplication operator,substraction operator, and divide operator).
friend ostream& operator<<(ostream& os, matrix& m);
matrix operator + (matrix&);
matrix operator * (matrix&);
matrix operator - (matrix&);
matrix operator / (matrix&);
private:
void init(int, int);
int nrows,ncols;
double *data;
};
matrix::matrix(){
init(1,1);
}
matrix::matrix(int m, int n){
init(m,n);
}
//destructor to delete the used dynamic memory.
void matrix::init(int m, int n){
nrows=m;
ncols=n;
data= new double[m*n];
for(int i=0; i<m*n; i++)
data[i]=0;
}
int matrix::getRow() { return nrows;}
int matrix::getCol() { return ncols;}
double& matrix::operator ()(int r, int c){
if (r <0 || r> nrows){
cout<<"Illegal row index";
return data[0];
}
else if (c <0 || c > ncols){
cout<<"Illegal Column Index:";
return data[0];
}
else return data[r*ncols+c];
}
ostream& operator<<(ostream& os, matrix &m)
{
int mval=m.getRow();
int nval=m.getCol();
for(int i=0; i<mval; i++){
for(int j=0; j < nval; j++)
os<<m(i,j)<<" ";
os<<endl;
}
return os;
}
//To compute the summation
matrix matrix::operator+(matrix& a)
{
matrix sum(nrows, ncols);
for (int i=0; i<nrows; i++)
for(int j=0; j<ncols; j++)
sum(i,j) = (i,j) + a(i,j);
return sum;
}
//To compute the multiplication
matrix matrix::operator*(matrix& a)
{
matrix product(nrows, ncols);
for (int i=0; i<nrows; i++)
for(int j=0; j<ncols; j++)
product(i,j) = (i,j) * a(i,j);
return product;
}
//To compute the substraction
matrix matrix::operator-(matrix& a)
{
matrix difference(nrows, ncols);
for (int i=0; i<nrows; i++)
for(int j=0; j<ncols; j++)
difference(i,j) = (i,j) - a(i,j);
return difference;
}
//To compute the division
matrix matrix::operator/(matrix& a)
{
matrix divide(nrows, ncols);
for (int i=0; i<nrows; i++)
for(int j=0; j<ncols; j++)
divide(i,j) = (i,j) / a(i,j);
return divide;
}
int main()
{
//the array of matrix a
matrix a(2,2);
a(1,1)=5.0;
a(0,0)=6.0;
a(0,1)=7.0;
a(1,0)=8.0;
cout<<"matrix a\n";
cout<<a<<endl;
//the array of matrix b
matrix b(2,2);
b(0,0) = 5.0;
b(0,1) = 5.0;
b(1,0) = 5.0;
b(1,1) = 5.0;
cout<<"matrix b\n";
cout<<b<<endl;
matrix c,d,e,f;
//to compute the operation of the matrix (addition, multiplication, substraction and division).
c=a+b;
d=a*b;
e=a-b;
f=a/b;
//to Display the result operation of the matrix (addition, multiplication, substraction and division).
cout<<"matrix a+b \n";
cout<<c<<endl;
cout <<"\n\n";
cout<<"matrix a*b\n";
cout<<d<<endl;
cout<<"matrix a-b\n";
cout<<e<<endl;
cout <<"\n\n";
cout<<"matrix a/b\n";
cout<<f<<endl;
return 0;
}
ANSWER: Hello Steve.
The reason the code is not giving the expected results is that the call to this->operator()(int,int) is not actually being made in the matrix methods . Where you have
sum(i,j) = (i,j) + a(i,j)
you need
sum(i,j) = (*this)(i,j) + a(i,j)
or
sum(i,j) = this->operator(i,j) + a(i,j)
The same correction has to be made with all the other matrix math methods.
Now, a few more comments. The matrix addition and matrix subtraction are correct, but you should handle the case where the “this” matrix and the “a” matrix are of different sizes. In real matrix algebra, such additions and subtractions cannot be done. Even in your system, you should check for size differences and handle them. The correct thing to do would be to throw an exception if the operation cannot be done. If you have not studied exceptions yet, then I suspect your instructor does not expect you to use them and you could just print an error message.
My comment about sizes applies to your division and multiplication methods as well. You should know that your division and multiplication methods are not correct matrix algebra. Does your instructor want correct matrix algebra done ? If so, you should try to learn the methods. Proper matrix multiplication is easy and is explained in the first few paragraphs of
http://en.wikipedia.org/wiki/Matrix_multiplication
Again you have to pay attention to matrix sizes. When doing A*B in matrix algebra, the number of columns in A must match the number of rows in B, otherwise the multiplication cannot be done.
Matrix division A/B is A * B inverse. I don’t remember how to take the inverse of a matrix but If you need help, I’ll re-learn it.
In your current system, the matrix division will cause a program crash if “a(i,j)” is 0 and you should check before doing the division operation. Again, some sort of error handling should be done.
I hope that helps you.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: Tks for telling me about exception. I am still unsure about "this->". It is a pointer used in class right?
Some qns:
When I declare (*this)(i,j) + a(i,j) below, it works! But this -> operator(i,j) + a(i,j) do not work.
matrix matrix::operator+(matrix& a)
{
matrix sum(rows, cols);
for (int i=0; i < rows; i++)
for(int j=0; j < cols; j++)
sum(i,j) = (*this)(i,j) + a(i,j);
return sum;
}
sum(i,j) = this -> operator(i,j) + a(i,j); //When declare inside above code fragment, program don't work. Is it must declare *this?
AnswerHello Steve
The "this" pointer points to the object whose methods you are running. Each object has a this pointer. You do not need to declare it. When you are in an object's method, you can access all the object's data and functions through the this pointer, although usually you don't need to specify the this pointer.
Sorry, I gave you the wrong syntax.
Try
this->operator()(i,j);
Notice the empty parentheses before (i,j)
Best regards
Zlatko